trace_osnoise.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OS Noise Tracer: computes the OS Noise suffered by a running thread.
  4. * Timerlat Tracer: measures the wakeup latency of a timer triggered IRQ and thread.
  5. *
  6. * Based on "hwlat_detector" tracer by:
  7. * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  8. * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com>
  9. * With feedback from Clark Williams <williams@redhat.com>
  10. *
  11. * And also based on the rtsl tracer presented on:
  12. * DE OLIVEIRA, Daniel Bristot, et al. Demystifying the real-time linux
  13. * scheduling latency. In: 32nd Euromicro Conference on Real-Time Systems
  14. * (ECRTS 2020). Schloss Dagstuhl-Leibniz-Zentrum fur Informatik, 2020.
  15. *
  16. * Copyright (C) 2021 Daniel Bristot de Oliveira, Red Hat, Inc. <bristot@redhat.com>
  17. */
  18. #include <linux/kthread.h>
  19. #include <linux/tracefs.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched/clock.h>
  24. #include <uapi/linux/sched/types.h>
  25. #include <linux/sched.h>
  26. #include "trace.h"
  27. #ifdef CONFIG_X86_LOCAL_APIC
  28. #include <asm/trace/irq_vectors.h>
  29. #undef TRACE_INCLUDE_PATH
  30. #undef TRACE_INCLUDE_FILE
  31. #endif /* CONFIG_X86_LOCAL_APIC */
  32. #include <trace/events/irq.h>
  33. #include <trace/events/sched.h>
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/osnoise.h>
  36. /*
  37. * Default values.
  38. */
  39. #define BANNER "osnoise: "
  40. #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */
  41. #define DEFAULT_SAMPLE_RUNTIME 1000000 /* 1s */
  42. #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */
  43. #define DEFAULT_TIMERLAT_PRIO 95 /* FIFO 95 */
  44. /*
  45. * osnoise/options entries.
  46. */
  47. enum osnoise_options_index {
  48. OSN_DEFAULTS = 0,
  49. OSN_WORKLOAD,
  50. OSN_PANIC_ON_STOP,
  51. OSN_PREEMPT_DISABLE,
  52. OSN_IRQ_DISABLE,
  53. OSN_MAX
  54. };
  55. static const char * const osnoise_options_str[OSN_MAX] = {
  56. "DEFAULTS",
  57. "OSNOISE_WORKLOAD",
  58. "PANIC_ON_STOP",
  59. "OSNOISE_PREEMPT_DISABLE",
  60. "OSNOISE_IRQ_DISABLE" };
  61. #define OSN_DEFAULT_OPTIONS 0x2
  62. static unsigned long osnoise_options = OSN_DEFAULT_OPTIONS;
  63. /*
  64. * trace_array of the enabled osnoise/timerlat instances.
  65. */
  66. struct osnoise_instance {
  67. struct list_head list;
  68. struct trace_array *tr;
  69. };
  70. static struct list_head osnoise_instances;
  71. static bool osnoise_has_registered_instances(void)
  72. {
  73. return !!list_first_or_null_rcu(&osnoise_instances,
  74. struct osnoise_instance,
  75. list);
  76. }
  77. /*
  78. * osnoise_instance_registered - check if a tr is already registered
  79. */
  80. static int osnoise_instance_registered(struct trace_array *tr)
  81. {
  82. struct osnoise_instance *inst;
  83. int found = 0;
  84. rcu_read_lock();
  85. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  86. if (inst->tr == tr)
  87. found = 1;
  88. }
  89. rcu_read_unlock();
  90. return found;
  91. }
  92. /*
  93. * osnoise_register_instance - register a new trace instance
  94. *
  95. * Register a trace_array *tr in the list of instances running
  96. * osnoise/timerlat tracers.
  97. */
  98. static int osnoise_register_instance(struct trace_array *tr)
  99. {
  100. struct osnoise_instance *inst;
  101. /*
  102. * register/unregister serialization is provided by trace's
  103. * trace_types_lock.
  104. */
  105. lockdep_assert_held(&trace_types_lock);
  106. inst = kmalloc(sizeof(*inst), GFP_KERNEL);
  107. if (!inst)
  108. return -ENOMEM;
  109. INIT_LIST_HEAD_RCU(&inst->list);
  110. inst->tr = tr;
  111. list_add_tail_rcu(&inst->list, &osnoise_instances);
  112. return 0;
  113. }
  114. /*
  115. * osnoise_unregister_instance - unregister a registered trace instance
  116. *
  117. * Remove the trace_array *tr from the list of instances running
  118. * osnoise/timerlat tracers.
  119. */
  120. static void osnoise_unregister_instance(struct trace_array *tr)
  121. {
  122. struct osnoise_instance *inst;
  123. int found = 0;
  124. /*
  125. * register/unregister serialization is provided by trace's
  126. * trace_types_lock.
  127. */
  128. list_for_each_entry_rcu(inst, &osnoise_instances, list,
  129. lockdep_is_held(&trace_types_lock)) {
  130. if (inst->tr == tr) {
  131. list_del_rcu(&inst->list);
  132. found = 1;
  133. break;
  134. }
  135. }
  136. if (!found)
  137. return;
  138. kvfree_rcu_mightsleep(inst);
  139. }
  140. /*
  141. * NMI runtime info.
  142. */
  143. struct osn_nmi {
  144. u64 count;
  145. u64 delta_start;
  146. };
  147. /*
  148. * IRQ runtime info.
  149. */
  150. struct osn_irq {
  151. u64 count;
  152. u64 arrival_time;
  153. u64 delta_start;
  154. };
  155. #define IRQ_CONTEXT 0
  156. #define THREAD_CONTEXT 1
  157. #define THREAD_URET 2
  158. /*
  159. * sofirq runtime info.
  160. */
  161. struct osn_softirq {
  162. u64 count;
  163. u64 arrival_time;
  164. u64 delta_start;
  165. };
  166. /*
  167. * thread runtime info.
  168. */
  169. struct osn_thread {
  170. u64 count;
  171. u64 arrival_time;
  172. u64 delta_start;
  173. };
  174. /*
  175. * Runtime information: this structure saves the runtime information used by
  176. * one sampling thread.
  177. */
  178. struct osnoise_variables {
  179. struct task_struct *kthread;
  180. bool sampling;
  181. pid_t pid;
  182. struct osn_nmi nmi;
  183. struct osn_irq irq;
  184. struct osn_softirq softirq;
  185. struct osn_thread thread;
  186. local_t int_counter;
  187. };
  188. /*
  189. * Per-cpu runtime information.
  190. */
  191. static DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var);
  192. /*
  193. * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU
  194. */
  195. static inline struct osnoise_variables *this_cpu_osn_var(void)
  196. {
  197. return this_cpu_ptr(&per_cpu_osnoise_var);
  198. }
  199. /*
  200. * Protect the interface.
  201. */
  202. static struct mutex interface_lock;
  203. #ifdef CONFIG_TIMERLAT_TRACER
  204. /*
  205. * Runtime information for the timer mode.
  206. */
  207. struct timerlat_variables {
  208. struct task_struct *kthread;
  209. struct hrtimer timer;
  210. u64 rel_period;
  211. u64 abs_period;
  212. bool tracing_thread;
  213. u64 count;
  214. bool uthread_migrate;
  215. };
  216. static DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var);
  217. /*
  218. * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU
  219. */
  220. static inline struct timerlat_variables *this_cpu_tmr_var(void)
  221. {
  222. return this_cpu_ptr(&per_cpu_timerlat_var);
  223. }
  224. /*
  225. * tlat_var_reset - Reset the values of the given timerlat_variables
  226. */
  227. static inline void tlat_var_reset(void)
  228. {
  229. struct timerlat_variables *tlat_var;
  230. int cpu;
  231. /* Synchronize with the timerlat interfaces */
  232. mutex_lock(&interface_lock);
  233. /*
  234. * So far, all the values are initialized as 0, so
  235. * zeroing the structure is perfect.
  236. */
  237. for_each_cpu(cpu, cpu_online_mask) {
  238. tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
  239. if (tlat_var->kthread)
  240. hrtimer_cancel(&tlat_var->timer);
  241. memset(tlat_var, 0, sizeof(*tlat_var));
  242. }
  243. mutex_unlock(&interface_lock);
  244. }
  245. #else /* CONFIG_TIMERLAT_TRACER */
  246. #define tlat_var_reset() do {} while (0)
  247. #endif /* CONFIG_TIMERLAT_TRACER */
  248. /*
  249. * osn_var_reset - Reset the values of the given osnoise_variables
  250. */
  251. static inline void osn_var_reset(void)
  252. {
  253. struct osnoise_variables *osn_var;
  254. int cpu;
  255. /*
  256. * So far, all the values are initialized as 0, so
  257. * zeroing the structure is perfect.
  258. */
  259. for_each_cpu(cpu, cpu_online_mask) {
  260. osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
  261. memset(osn_var, 0, sizeof(*osn_var));
  262. }
  263. }
  264. /*
  265. * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables
  266. */
  267. static inline void osn_var_reset_all(void)
  268. {
  269. osn_var_reset();
  270. tlat_var_reset();
  271. }
  272. /*
  273. * Tells NMIs to call back to the osnoise tracer to record timestamps.
  274. */
  275. bool trace_osnoise_callback_enabled;
  276. /*
  277. * osnoise sample structure definition. Used to store the statistics of a
  278. * sample run.
  279. */
  280. struct osnoise_sample {
  281. u64 runtime; /* runtime */
  282. u64 noise; /* noise */
  283. u64 max_sample; /* max single noise sample */
  284. int hw_count; /* # HW (incl. hypervisor) interference */
  285. int nmi_count; /* # NMIs during this sample */
  286. int irq_count; /* # IRQs during this sample */
  287. int softirq_count; /* # softirqs during this sample */
  288. int thread_count; /* # threads during this sample */
  289. };
  290. #ifdef CONFIG_TIMERLAT_TRACER
  291. /*
  292. * timerlat sample structure definition. Used to store the statistics of
  293. * a sample run.
  294. */
  295. struct timerlat_sample {
  296. u64 timer_latency; /* timer_latency */
  297. unsigned int seqnum; /* unique sequence */
  298. int context; /* timer context */
  299. };
  300. #endif
  301. /*
  302. * Tracer data.
  303. */
  304. static struct osnoise_data {
  305. u64 sample_period; /* total sampling period */
  306. u64 sample_runtime; /* active sampling portion of period */
  307. u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */
  308. u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */
  309. #ifdef CONFIG_TIMERLAT_TRACER
  310. u64 timerlat_period; /* timerlat period */
  311. u64 print_stack; /* print IRQ stack if total > */
  312. int timerlat_tracer; /* timerlat tracer */
  313. #endif
  314. bool tainted; /* infor users and developers about a problem */
  315. } osnoise_data = {
  316. .sample_period = DEFAULT_SAMPLE_PERIOD,
  317. .sample_runtime = DEFAULT_SAMPLE_RUNTIME,
  318. .stop_tracing = 0,
  319. .stop_tracing_total = 0,
  320. #ifdef CONFIG_TIMERLAT_TRACER
  321. .print_stack = 0,
  322. .timerlat_period = DEFAULT_TIMERLAT_PERIOD,
  323. .timerlat_tracer = 0,
  324. #endif
  325. };
  326. #ifdef CONFIG_TIMERLAT_TRACER
  327. static inline bool timerlat_enabled(void)
  328. {
  329. return osnoise_data.timerlat_tracer;
  330. }
  331. static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
  332. {
  333. struct timerlat_variables *tlat_var = this_cpu_tmr_var();
  334. /*
  335. * If the timerlat is enabled, but the irq handler did
  336. * not run yet enabling timerlat_tracer, do not trace.
  337. */
  338. if (!tlat_var->tracing_thread) {
  339. osn_var->softirq.arrival_time = 0;
  340. osn_var->softirq.delta_start = 0;
  341. return 0;
  342. }
  343. return 1;
  344. }
  345. static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
  346. {
  347. struct timerlat_variables *tlat_var = this_cpu_tmr_var();
  348. /*
  349. * If the timerlat is enabled, but the irq handler did
  350. * not run yet enabling timerlat_tracer, do not trace.
  351. */
  352. if (!tlat_var->tracing_thread) {
  353. osn_var->thread.delta_start = 0;
  354. osn_var->thread.arrival_time = 0;
  355. return 0;
  356. }
  357. return 1;
  358. }
  359. #else /* CONFIG_TIMERLAT_TRACER */
  360. static inline bool timerlat_enabled(void)
  361. {
  362. return false;
  363. }
  364. static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
  365. {
  366. return 1;
  367. }
  368. static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
  369. {
  370. return 1;
  371. }
  372. #endif
  373. #ifdef CONFIG_PREEMPT_RT
  374. /*
  375. * Print the osnoise header info.
  376. */
  377. static void print_osnoise_headers(struct seq_file *s)
  378. {
  379. if (osnoise_data.tainted)
  380. seq_puts(s, "# osnoise is tainted!\n");
  381. seq_puts(s, "# _-------=> irqs-off\n");
  382. seq_puts(s, "# / _------=> need-resched\n");
  383. seq_puts(s, "# | / _-----=> need-resched-lazy\n");
  384. seq_puts(s, "# || / _----=> hardirq/softirq\n");
  385. seq_puts(s, "# ||| / _---=> preempt-depth\n");
  386. seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
  387. seq_puts(s, "# ||||| / _-=> migrate-disable\n");
  388. seq_puts(s, "# |||||| / ");
  389. seq_puts(s, " MAX\n");
  390. seq_puts(s, "# ||||| / ");
  391. seq_puts(s, " SINGLE Interference counters:\n");
  392. seq_puts(s, "# ||||||| RUNTIME ");
  393. seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
  394. seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US ");
  395. seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
  396. seq_puts(s, "# | | | ||||||| | | ");
  397. seq_puts(s, " | | | | | | | |\n");
  398. }
  399. #else /* CONFIG_PREEMPT_RT */
  400. static void print_osnoise_headers(struct seq_file *s)
  401. {
  402. if (osnoise_data.tainted)
  403. seq_puts(s, "# osnoise is tainted!\n");
  404. seq_puts(s, "# _-----=> irqs-off\n");
  405. seq_puts(s, "# / _----=> need-resched\n");
  406. seq_puts(s, "# | / _---=> hardirq/softirq\n");
  407. seq_puts(s, "# || / _--=> preempt-depth\n");
  408. seq_puts(s, "# ||| / _-=> migrate-disable ");
  409. seq_puts(s, " MAX\n");
  410. seq_puts(s, "# |||| / delay ");
  411. seq_puts(s, " SINGLE Interference counters:\n");
  412. seq_puts(s, "# ||||| RUNTIME ");
  413. seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
  414. seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US ");
  415. seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
  416. seq_puts(s, "# | | | ||||| | | ");
  417. seq_puts(s, " | | | | | | | |\n");
  418. }
  419. #endif /* CONFIG_PREEMPT_RT */
  420. /*
  421. * osnoise_taint - report an osnoise error.
  422. */
  423. #define osnoise_taint(msg) ({ \
  424. struct osnoise_instance *inst; \
  425. struct trace_buffer *buffer; \
  426. \
  427. rcu_read_lock(); \
  428. list_for_each_entry_rcu(inst, &osnoise_instances, list) { \
  429. buffer = inst->tr->array_buffer.buffer; \
  430. trace_array_printk_buf(buffer, _THIS_IP_, msg); \
  431. } \
  432. rcu_read_unlock(); \
  433. osnoise_data.tainted = true; \
  434. })
  435. /*
  436. * Record an osnoise_sample into the tracer buffer.
  437. */
  438. static void
  439. __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer)
  440. {
  441. struct trace_event_call *call = &event_osnoise;
  442. struct ring_buffer_event *event;
  443. struct osnoise_entry *entry;
  444. event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry),
  445. tracing_gen_ctx());
  446. if (!event)
  447. return;
  448. entry = ring_buffer_event_data(event);
  449. entry->runtime = sample->runtime;
  450. entry->noise = sample->noise;
  451. entry->max_sample = sample->max_sample;
  452. entry->hw_count = sample->hw_count;
  453. entry->nmi_count = sample->nmi_count;
  454. entry->irq_count = sample->irq_count;
  455. entry->softirq_count = sample->softirq_count;
  456. entry->thread_count = sample->thread_count;
  457. if (!call_filter_check_discard(call, entry, buffer, event))
  458. trace_buffer_unlock_commit_nostack(buffer, event);
  459. }
  460. /*
  461. * Record an osnoise_sample on all osnoise instances.
  462. */
  463. static void trace_osnoise_sample(struct osnoise_sample *sample)
  464. {
  465. struct osnoise_instance *inst;
  466. struct trace_buffer *buffer;
  467. rcu_read_lock();
  468. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  469. buffer = inst->tr->array_buffer.buffer;
  470. __trace_osnoise_sample(sample, buffer);
  471. }
  472. rcu_read_unlock();
  473. }
  474. #ifdef CONFIG_TIMERLAT_TRACER
  475. /*
  476. * Print the timerlat header info.
  477. */
  478. #ifdef CONFIG_PREEMPT_RT
  479. static void print_timerlat_headers(struct seq_file *s)
  480. {
  481. seq_puts(s, "# _-------=> irqs-off\n");
  482. seq_puts(s, "# / _------=> need-resched\n");
  483. seq_puts(s, "# | / _-----=> need-resched-lazy\n");
  484. seq_puts(s, "# || / _----=> hardirq/softirq\n");
  485. seq_puts(s, "# ||| / _---=> preempt-depth\n");
  486. seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
  487. seq_puts(s, "# ||||| / _-=> migrate-disable\n");
  488. seq_puts(s, "# |||||| /\n");
  489. seq_puts(s, "# ||||||| ACTIVATION\n");
  490. seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID ");
  491. seq_puts(s, " CONTEXT LATENCY\n");
  492. seq_puts(s, "# | | | ||||||| | | ");
  493. seq_puts(s, " | |\n");
  494. }
  495. #else /* CONFIG_PREEMPT_RT */
  496. static void print_timerlat_headers(struct seq_file *s)
  497. {
  498. seq_puts(s, "# _-----=> irqs-off\n");
  499. seq_puts(s, "# / _----=> need-resched\n");
  500. seq_puts(s, "# | / _---=> hardirq/softirq\n");
  501. seq_puts(s, "# || / _--=> preempt-depth\n");
  502. seq_puts(s, "# ||| / _-=> migrate-disable\n");
  503. seq_puts(s, "# |||| / delay\n");
  504. seq_puts(s, "# ||||| ACTIVATION\n");
  505. seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID ");
  506. seq_puts(s, " CONTEXT LATENCY\n");
  507. seq_puts(s, "# | | | ||||| | | ");
  508. seq_puts(s, " | |\n");
  509. }
  510. #endif /* CONFIG_PREEMPT_RT */
  511. static void
  512. __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer)
  513. {
  514. struct trace_event_call *call = &event_osnoise;
  515. struct ring_buffer_event *event;
  516. struct timerlat_entry *entry;
  517. event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry),
  518. tracing_gen_ctx());
  519. if (!event)
  520. return;
  521. entry = ring_buffer_event_data(event);
  522. entry->seqnum = sample->seqnum;
  523. entry->context = sample->context;
  524. entry->timer_latency = sample->timer_latency;
  525. if (!call_filter_check_discard(call, entry, buffer, event))
  526. trace_buffer_unlock_commit_nostack(buffer, event);
  527. }
  528. /*
  529. * Record an timerlat_sample into the tracer buffer.
  530. */
  531. static void trace_timerlat_sample(struct timerlat_sample *sample)
  532. {
  533. struct osnoise_instance *inst;
  534. struct trace_buffer *buffer;
  535. rcu_read_lock();
  536. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  537. buffer = inst->tr->array_buffer.buffer;
  538. __trace_timerlat_sample(sample, buffer);
  539. }
  540. rcu_read_unlock();
  541. }
  542. #ifdef CONFIG_STACKTRACE
  543. #define MAX_CALLS 256
  544. /*
  545. * Stack trace will take place only at IRQ level, so, no need
  546. * to control nesting here.
  547. */
  548. struct trace_stack {
  549. int stack_size;
  550. int nr_entries;
  551. unsigned long calls[MAX_CALLS];
  552. };
  553. static DEFINE_PER_CPU(struct trace_stack, trace_stack);
  554. /*
  555. * timerlat_save_stack - save a stack trace without printing
  556. *
  557. * Save the current stack trace without printing. The
  558. * stack will be printed later, after the end of the measurement.
  559. */
  560. static void timerlat_save_stack(int skip)
  561. {
  562. unsigned int size, nr_entries;
  563. struct trace_stack *fstack;
  564. fstack = this_cpu_ptr(&trace_stack);
  565. size = ARRAY_SIZE(fstack->calls);
  566. nr_entries = stack_trace_save(fstack->calls, size, skip);
  567. fstack->stack_size = nr_entries * sizeof(unsigned long);
  568. fstack->nr_entries = nr_entries;
  569. return;
  570. }
  571. static void
  572. __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size)
  573. {
  574. struct trace_event_call *call = &event_osnoise;
  575. struct ring_buffer_event *event;
  576. struct stack_entry *entry;
  577. event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size,
  578. tracing_gen_ctx());
  579. if (!event)
  580. return;
  581. entry = ring_buffer_event_data(event);
  582. entry->size = fstack->nr_entries;
  583. memcpy(&entry->caller, fstack->calls, size);
  584. if (!call_filter_check_discard(call, entry, buffer, event))
  585. trace_buffer_unlock_commit_nostack(buffer, event);
  586. }
  587. /*
  588. * timerlat_dump_stack - dump a stack trace previously saved
  589. */
  590. static void timerlat_dump_stack(u64 latency)
  591. {
  592. struct osnoise_instance *inst;
  593. struct trace_buffer *buffer;
  594. struct trace_stack *fstack;
  595. unsigned int size;
  596. /*
  597. * trace only if latency > print_stack config, if enabled.
  598. */
  599. if (!osnoise_data.print_stack || osnoise_data.print_stack > latency)
  600. return;
  601. preempt_disable_notrace();
  602. fstack = this_cpu_ptr(&trace_stack);
  603. size = fstack->stack_size;
  604. rcu_read_lock();
  605. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  606. buffer = inst->tr->array_buffer.buffer;
  607. __timerlat_dump_stack(buffer, fstack, size);
  608. }
  609. rcu_read_unlock();
  610. preempt_enable_notrace();
  611. }
  612. #else /* CONFIG_STACKTRACE */
  613. #define timerlat_dump_stack(u64 latency) do {} while (0)
  614. #define timerlat_save_stack(a) do {} while (0)
  615. #endif /* CONFIG_STACKTRACE */
  616. #endif /* CONFIG_TIMERLAT_TRACER */
  617. /*
  618. * Macros to encapsulate the time capturing infrastructure.
  619. */
  620. #define time_get() trace_clock_local()
  621. #define time_to_us(x) div_u64(x, 1000)
  622. #define time_sub(a, b) ((a) - (b))
  623. /*
  624. * cond_move_irq_delta_start - Forward the delta_start of a running IRQ
  625. *
  626. * If an IRQ is preempted by an NMI, its delta_start is pushed forward
  627. * to discount the NMI interference.
  628. *
  629. * See get_int_safe_duration().
  630. */
  631. static inline void
  632. cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration)
  633. {
  634. if (osn_var->irq.delta_start)
  635. osn_var->irq.delta_start += duration;
  636. }
  637. #ifndef CONFIG_PREEMPT_RT
  638. /*
  639. * cond_move_softirq_delta_start - Forward the delta_start of a running softirq.
  640. *
  641. * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed
  642. * forward to discount the interference.
  643. *
  644. * See get_int_safe_duration().
  645. */
  646. static inline void
  647. cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration)
  648. {
  649. if (osn_var->softirq.delta_start)
  650. osn_var->softirq.delta_start += duration;
  651. }
  652. #else /* CONFIG_PREEMPT_RT */
  653. #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0)
  654. #endif
  655. /*
  656. * cond_move_thread_delta_start - Forward the delta_start of a running thread
  657. *
  658. * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start
  659. * is pushed forward to discount the interference.
  660. *
  661. * See get_int_safe_duration().
  662. */
  663. static inline void
  664. cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration)
  665. {
  666. if (osn_var->thread.delta_start)
  667. osn_var->thread.delta_start += duration;
  668. }
  669. /*
  670. * get_int_safe_duration - Get the duration of a window
  671. *
  672. * The irq, softirq and thread varaibles need to have its duration without
  673. * the interference from higher priority interrupts. Instead of keeping a
  674. * variable to discount the interrupt interference from these variables, the
  675. * starting time of these variables are pushed forward with the interrupt's
  676. * duration. In this way, a single variable is used to:
  677. *
  678. * - Know if a given window is being measured.
  679. * - Account its duration.
  680. * - Discount the interference.
  681. *
  682. * To avoid getting inconsistent values, e.g.,:
  683. *
  684. * now = time_get()
  685. * ---> interrupt!
  686. * delta_start -= int duration;
  687. * <---
  688. * duration = now - delta_start;
  689. *
  690. * result: negative duration if the variable duration before the
  691. * interrupt was smaller than the interrupt execution.
  692. *
  693. * A counter of interrupts is used. If the counter increased, try
  694. * to capture an interference safe duration.
  695. */
  696. static inline s64
  697. get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start)
  698. {
  699. u64 int_counter, now;
  700. s64 duration;
  701. do {
  702. int_counter = local_read(&osn_var->int_counter);
  703. /* synchronize with interrupts */
  704. barrier();
  705. now = time_get();
  706. duration = (now - *delta_start);
  707. /* synchronize with interrupts */
  708. barrier();
  709. } while (int_counter != local_read(&osn_var->int_counter));
  710. /*
  711. * This is an evidence of race conditions that cause
  712. * a value to be "discounted" too much.
  713. */
  714. if (duration < 0)
  715. osnoise_taint("Negative duration!\n");
  716. *delta_start = 0;
  717. return duration;
  718. }
  719. /*
  720. *
  721. * set_int_safe_time - Save the current time on *time, aware of interference
  722. *
  723. * Get the time, taking into consideration a possible interference from
  724. * higher priority interrupts.
  725. *
  726. * See get_int_safe_duration() for an explanation.
  727. */
  728. static u64
  729. set_int_safe_time(struct osnoise_variables *osn_var, u64 *time)
  730. {
  731. u64 int_counter;
  732. do {
  733. int_counter = local_read(&osn_var->int_counter);
  734. /* synchronize with interrupts */
  735. barrier();
  736. *time = time_get();
  737. /* synchronize with interrupts */
  738. barrier();
  739. } while (int_counter != local_read(&osn_var->int_counter));
  740. return int_counter;
  741. }
  742. #ifdef CONFIG_TIMERLAT_TRACER
  743. /*
  744. * copy_int_safe_time - Copy *src into *desc aware of interference
  745. */
  746. static u64
  747. copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src)
  748. {
  749. u64 int_counter;
  750. do {
  751. int_counter = local_read(&osn_var->int_counter);
  752. /* synchronize with interrupts */
  753. barrier();
  754. *dst = *src;
  755. /* synchronize with interrupts */
  756. barrier();
  757. } while (int_counter != local_read(&osn_var->int_counter));
  758. return int_counter;
  759. }
  760. #endif /* CONFIG_TIMERLAT_TRACER */
  761. /*
  762. * trace_osnoise_callback - NMI entry/exit callback
  763. *
  764. * This function is called at the entry and exit NMI code. The bool enter
  765. * distinguishes between either case. This function is used to note a NMI
  766. * occurrence, compute the noise caused by the NMI, and to remove the noise
  767. * it is potentially causing on other interference variables.
  768. */
  769. void trace_osnoise_callback(bool enter)
  770. {
  771. struct osnoise_variables *osn_var = this_cpu_osn_var();
  772. u64 duration;
  773. if (!osn_var->sampling)
  774. return;
  775. /*
  776. * Currently trace_clock_local() calls sched_clock() and the
  777. * generic version is not NMI safe.
  778. */
  779. if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
  780. if (enter) {
  781. osn_var->nmi.delta_start = time_get();
  782. local_inc(&osn_var->int_counter);
  783. } else {
  784. duration = time_get() - osn_var->nmi.delta_start;
  785. trace_nmi_noise(osn_var->nmi.delta_start, duration);
  786. cond_move_irq_delta_start(osn_var, duration);
  787. cond_move_softirq_delta_start(osn_var, duration);
  788. cond_move_thread_delta_start(osn_var, duration);
  789. }
  790. }
  791. if (enter)
  792. osn_var->nmi.count++;
  793. }
  794. /*
  795. * osnoise_trace_irq_entry - Note the starting of an IRQ
  796. *
  797. * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs,
  798. * it is safe to use a single variable (ons_var->irq) to save the statistics.
  799. * The arrival_time is used to report... the arrival time. The delta_start
  800. * is used to compute the duration at the IRQ exit handler. See
  801. * cond_move_irq_delta_start().
  802. */
  803. void osnoise_trace_irq_entry(int id)
  804. {
  805. struct osnoise_variables *osn_var = this_cpu_osn_var();
  806. if (!osn_var->sampling)
  807. return;
  808. /*
  809. * This value will be used in the report, but not to compute
  810. * the execution time, so it is safe to get it unsafe.
  811. */
  812. osn_var->irq.arrival_time = time_get();
  813. set_int_safe_time(osn_var, &osn_var->irq.delta_start);
  814. osn_var->irq.count++;
  815. local_inc(&osn_var->int_counter);
  816. }
  817. /*
  818. * osnoise_irq_exit - Note the end of an IRQ, sava data and trace
  819. *
  820. * Computes the duration of the IRQ noise, and trace it. Also discounts the
  821. * interference from other sources of noise could be currently being accounted.
  822. */
  823. void osnoise_trace_irq_exit(int id, const char *desc)
  824. {
  825. struct osnoise_variables *osn_var = this_cpu_osn_var();
  826. s64 duration;
  827. if (!osn_var->sampling)
  828. return;
  829. duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start);
  830. trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration);
  831. osn_var->irq.arrival_time = 0;
  832. cond_move_softirq_delta_start(osn_var, duration);
  833. cond_move_thread_delta_start(osn_var, duration);
  834. }
  835. /*
  836. * trace_irqentry_callback - Callback to the irq:irq_entry traceevent
  837. *
  838. * Used to note the starting of an IRQ occurece.
  839. */
  840. static void trace_irqentry_callback(void *data, int irq,
  841. struct irqaction *action)
  842. {
  843. osnoise_trace_irq_entry(irq);
  844. }
  845. /*
  846. * trace_irqexit_callback - Callback to the irq:irq_exit traceevent
  847. *
  848. * Used to note the end of an IRQ occurece.
  849. */
  850. static void trace_irqexit_callback(void *data, int irq,
  851. struct irqaction *action, int ret)
  852. {
  853. osnoise_trace_irq_exit(irq, action->name);
  854. }
  855. /*
  856. * arch specific register function.
  857. */
  858. int __weak osnoise_arch_register(void)
  859. {
  860. return 0;
  861. }
  862. /*
  863. * arch specific unregister function.
  864. */
  865. void __weak osnoise_arch_unregister(void)
  866. {
  867. return;
  868. }
  869. /*
  870. * hook_irq_events - Hook IRQ handling events
  871. *
  872. * This function hooks the IRQ related callbacks to the respective trace
  873. * events.
  874. */
  875. static int hook_irq_events(void)
  876. {
  877. int ret;
  878. ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL);
  879. if (ret)
  880. goto out_err;
  881. ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL);
  882. if (ret)
  883. goto out_unregister_entry;
  884. ret = osnoise_arch_register();
  885. if (ret)
  886. goto out_irq_exit;
  887. return 0;
  888. out_irq_exit:
  889. unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
  890. out_unregister_entry:
  891. unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
  892. out_err:
  893. return -EINVAL;
  894. }
  895. /*
  896. * unhook_irq_events - Unhook IRQ handling events
  897. *
  898. * This function unhooks the IRQ related callbacks to the respective trace
  899. * events.
  900. */
  901. static void unhook_irq_events(void)
  902. {
  903. osnoise_arch_unregister();
  904. unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
  905. unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
  906. }
  907. #ifndef CONFIG_PREEMPT_RT
  908. /*
  909. * trace_softirq_entry_callback - Note the starting of a softirq
  910. *
  911. * Save the starting time of a softirq. As softirqs are non-preemptive to
  912. * other softirqs, it is safe to use a single variable (ons_var->softirq)
  913. * to save the statistics. The arrival_time is used to report... the
  914. * arrival time. The delta_start is used to compute the duration at the
  915. * softirq exit handler. See cond_move_softirq_delta_start().
  916. */
  917. static void trace_softirq_entry_callback(void *data, unsigned int vec_nr)
  918. {
  919. struct osnoise_variables *osn_var = this_cpu_osn_var();
  920. if (!osn_var->sampling)
  921. return;
  922. /*
  923. * This value will be used in the report, but not to compute
  924. * the execution time, so it is safe to get it unsafe.
  925. */
  926. osn_var->softirq.arrival_time = time_get();
  927. set_int_safe_time(osn_var, &osn_var->softirq.delta_start);
  928. osn_var->softirq.count++;
  929. local_inc(&osn_var->int_counter);
  930. }
  931. /*
  932. * trace_softirq_exit_callback - Note the end of an softirq
  933. *
  934. * Computes the duration of the softirq noise, and trace it. Also discounts the
  935. * interference from other sources of noise could be currently being accounted.
  936. */
  937. static void trace_softirq_exit_callback(void *data, unsigned int vec_nr)
  938. {
  939. struct osnoise_variables *osn_var = this_cpu_osn_var();
  940. s64 duration;
  941. if (!osn_var->sampling)
  942. return;
  943. if (unlikely(timerlat_enabled()))
  944. if (!timerlat_softirq_exit(osn_var))
  945. return;
  946. duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start);
  947. trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration);
  948. cond_move_thread_delta_start(osn_var, duration);
  949. osn_var->softirq.arrival_time = 0;
  950. }
  951. /*
  952. * hook_softirq_events - Hook softirq handling events
  953. *
  954. * This function hooks the softirq related callbacks to the respective trace
  955. * events.
  956. */
  957. static int hook_softirq_events(void)
  958. {
  959. int ret;
  960. ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL);
  961. if (ret)
  962. goto out_err;
  963. ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL);
  964. if (ret)
  965. goto out_unreg_entry;
  966. return 0;
  967. out_unreg_entry:
  968. unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
  969. out_err:
  970. return -EINVAL;
  971. }
  972. /*
  973. * unhook_softirq_events - Unhook softirq handling events
  974. *
  975. * This function hooks the softirq related callbacks to the respective trace
  976. * events.
  977. */
  978. static void unhook_softirq_events(void)
  979. {
  980. unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
  981. unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL);
  982. }
  983. #else /* CONFIG_PREEMPT_RT */
  984. /*
  985. * softirq are threads on the PREEMPT_RT mode.
  986. */
  987. static int hook_softirq_events(void)
  988. {
  989. return 0;
  990. }
  991. static void unhook_softirq_events(void)
  992. {
  993. }
  994. #endif
  995. /*
  996. * thread_entry - Record the starting of a thread noise window
  997. *
  998. * It saves the context switch time for a noisy thread, and increments
  999. * the interference counters.
  1000. */
  1001. static void
  1002. thread_entry(struct osnoise_variables *osn_var, struct task_struct *t)
  1003. {
  1004. if (!osn_var->sampling)
  1005. return;
  1006. /*
  1007. * The arrival time will be used in the report, but not to compute
  1008. * the execution time, so it is safe to get it unsafe.
  1009. */
  1010. osn_var->thread.arrival_time = time_get();
  1011. set_int_safe_time(osn_var, &osn_var->thread.delta_start);
  1012. osn_var->thread.count++;
  1013. local_inc(&osn_var->int_counter);
  1014. }
  1015. /*
  1016. * thread_exit - Report the end of a thread noise window
  1017. *
  1018. * It computes the total noise from a thread, tracing if needed.
  1019. */
  1020. static void
  1021. thread_exit(struct osnoise_variables *osn_var, struct task_struct *t)
  1022. {
  1023. s64 duration;
  1024. if (!osn_var->sampling)
  1025. return;
  1026. if (unlikely(timerlat_enabled()))
  1027. if (!timerlat_thread_exit(osn_var))
  1028. return;
  1029. duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start);
  1030. trace_thread_noise(t, osn_var->thread.arrival_time, duration);
  1031. osn_var->thread.arrival_time = 0;
  1032. }
  1033. #ifdef CONFIG_TIMERLAT_TRACER
  1034. /*
  1035. * osnoise_stop_exception - Stop tracing and the tracer.
  1036. */
  1037. static __always_inline void osnoise_stop_exception(char *msg, int cpu)
  1038. {
  1039. struct osnoise_instance *inst;
  1040. struct trace_array *tr;
  1041. rcu_read_lock();
  1042. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  1043. tr = inst->tr;
  1044. trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
  1045. "stop tracing hit on cpu %d due to exception: %s\n",
  1046. smp_processor_id(),
  1047. msg);
  1048. if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options))
  1049. panic("tracer hit on cpu %d due to exception: %s\n",
  1050. smp_processor_id(),
  1051. msg);
  1052. tracer_tracing_off(tr);
  1053. }
  1054. rcu_read_unlock();
  1055. }
  1056. /*
  1057. * trace_sched_migrate_callback - sched:sched_migrate_task trace event handler
  1058. *
  1059. * his function is hooked to the sched:sched_migrate_task trace event, and monitors
  1060. * timerlat user-space thread migration.
  1061. */
  1062. static void trace_sched_migrate_callback(void *data, struct task_struct *p, int dest_cpu)
  1063. {
  1064. struct osnoise_variables *osn_var;
  1065. long cpu = task_cpu(p);
  1066. osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
  1067. if (osn_var->pid == p->pid && dest_cpu != cpu) {
  1068. per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
  1069. osnoise_taint("timerlat user-thread migrated\n");
  1070. osnoise_stop_exception("timerlat user-thread migrated", cpu);
  1071. }
  1072. }
  1073. static bool monitor_enabled;
  1074. static int register_migration_monitor(void)
  1075. {
  1076. int ret = 0;
  1077. /*
  1078. * Timerlat thread migration check is only required when running timerlat in user-space.
  1079. * Thus, enable callback only if timerlat is set with no workload.
  1080. */
  1081. if (timerlat_enabled() && !test_bit(OSN_WORKLOAD, &osnoise_options)) {
  1082. if (WARN_ON_ONCE(monitor_enabled))
  1083. return 0;
  1084. ret = register_trace_sched_migrate_task(trace_sched_migrate_callback, NULL);
  1085. if (!ret)
  1086. monitor_enabled = true;
  1087. }
  1088. return ret;
  1089. }
  1090. static void unregister_migration_monitor(void)
  1091. {
  1092. if (!monitor_enabled)
  1093. return;
  1094. unregister_trace_sched_migrate_task(trace_sched_migrate_callback, NULL);
  1095. monitor_enabled = false;
  1096. }
  1097. #else
  1098. static int register_migration_monitor(void)
  1099. {
  1100. return 0;
  1101. }
  1102. static void unregister_migration_monitor(void) {}
  1103. #endif
  1104. /*
  1105. * trace_sched_switch - sched:sched_switch trace event handler
  1106. *
  1107. * This function is hooked to the sched:sched_switch trace event, and it is
  1108. * used to record the beginning and to report the end of a thread noise window.
  1109. */
  1110. static void
  1111. trace_sched_switch_callback(void *data, bool preempt,
  1112. struct task_struct *p,
  1113. struct task_struct *n,
  1114. unsigned int prev_state)
  1115. {
  1116. struct osnoise_variables *osn_var = this_cpu_osn_var();
  1117. int workload = test_bit(OSN_WORKLOAD, &osnoise_options);
  1118. if ((p->pid != osn_var->pid) || !workload)
  1119. thread_exit(osn_var, p);
  1120. if ((n->pid != osn_var->pid) || !workload)
  1121. thread_entry(osn_var, n);
  1122. }
  1123. /*
  1124. * hook_thread_events - Hook the instrumentation for thread noise
  1125. *
  1126. * Hook the osnoise tracer callbacks to handle the noise from other
  1127. * threads on the necessary kernel events.
  1128. */
  1129. static int hook_thread_events(void)
  1130. {
  1131. int ret;
  1132. ret = register_trace_sched_switch(trace_sched_switch_callback, NULL);
  1133. if (ret)
  1134. return -EINVAL;
  1135. ret = register_migration_monitor();
  1136. if (ret)
  1137. goto out_unreg;
  1138. return 0;
  1139. out_unreg:
  1140. unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
  1141. return -EINVAL;
  1142. }
  1143. /*
  1144. * unhook_thread_events - unhook the instrumentation for thread noise
  1145. *
  1146. * Unook the osnoise tracer callbacks to handle the noise from other
  1147. * threads on the necessary kernel events.
  1148. */
  1149. static void unhook_thread_events(void)
  1150. {
  1151. unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
  1152. unregister_migration_monitor();
  1153. }
  1154. /*
  1155. * save_osn_sample_stats - Save the osnoise_sample statistics
  1156. *
  1157. * Save the osnoise_sample statistics before the sampling phase. These
  1158. * values will be used later to compute the diff betwneen the statistics
  1159. * before and after the osnoise sampling.
  1160. */
  1161. static void
  1162. save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
  1163. {
  1164. s->nmi_count = osn_var->nmi.count;
  1165. s->irq_count = osn_var->irq.count;
  1166. s->softirq_count = osn_var->softirq.count;
  1167. s->thread_count = osn_var->thread.count;
  1168. }
  1169. /*
  1170. * diff_osn_sample_stats - Compute the osnoise_sample statistics
  1171. *
  1172. * After a sample period, compute the difference on the osnoise_sample
  1173. * statistics. The struct osnoise_sample *s contains the statistics saved via
  1174. * save_osn_sample_stats() before the osnoise sampling.
  1175. */
  1176. static void
  1177. diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
  1178. {
  1179. s->nmi_count = osn_var->nmi.count - s->nmi_count;
  1180. s->irq_count = osn_var->irq.count - s->irq_count;
  1181. s->softirq_count = osn_var->softirq.count - s->softirq_count;
  1182. s->thread_count = osn_var->thread.count - s->thread_count;
  1183. }
  1184. /*
  1185. * osnoise_stop_tracing - Stop tracing and the tracer.
  1186. */
  1187. static __always_inline void osnoise_stop_tracing(void)
  1188. {
  1189. struct osnoise_instance *inst;
  1190. struct trace_array *tr;
  1191. rcu_read_lock();
  1192. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  1193. tr = inst->tr;
  1194. trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
  1195. "stop tracing hit on cpu %d\n", smp_processor_id());
  1196. if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options))
  1197. panic("tracer hit stop condition on CPU %d\n", smp_processor_id());
  1198. tracer_tracing_off(tr);
  1199. }
  1200. rcu_read_unlock();
  1201. }
  1202. /*
  1203. * osnoise_has_tracing_on - Check if there is at least one instance on
  1204. */
  1205. static __always_inline int osnoise_has_tracing_on(void)
  1206. {
  1207. struct osnoise_instance *inst;
  1208. int trace_is_on = 0;
  1209. rcu_read_lock();
  1210. list_for_each_entry_rcu(inst, &osnoise_instances, list)
  1211. trace_is_on += tracer_tracing_is_on(inst->tr);
  1212. rcu_read_unlock();
  1213. return trace_is_on;
  1214. }
  1215. /*
  1216. * notify_new_max_latency - Notify a new max latency via fsnotify interface.
  1217. */
  1218. static void notify_new_max_latency(u64 latency)
  1219. {
  1220. struct osnoise_instance *inst;
  1221. struct trace_array *tr;
  1222. rcu_read_lock();
  1223. list_for_each_entry_rcu(inst, &osnoise_instances, list) {
  1224. tr = inst->tr;
  1225. if (tracer_tracing_is_on(tr) && tr->max_latency < latency) {
  1226. tr->max_latency = latency;
  1227. latency_fsnotify(tr);
  1228. }
  1229. }
  1230. rcu_read_unlock();
  1231. }
  1232. /*
  1233. * run_osnoise - Sample the time and look for osnoise
  1234. *
  1235. * Used to capture the time, looking for potential osnoise latency repeatedly.
  1236. * Different from hwlat_detector, it is called with preemption and interrupts
  1237. * enabled. This allows irqs, softirqs and threads to run, interfering on the
  1238. * osnoise sampling thread, as they would do with a regular thread.
  1239. */
  1240. static int run_osnoise(void)
  1241. {
  1242. bool disable_irq = test_bit(OSN_IRQ_DISABLE, &osnoise_options);
  1243. struct osnoise_variables *osn_var = this_cpu_osn_var();
  1244. u64 start, sample, last_sample;
  1245. u64 last_int_count, int_count;
  1246. s64 noise = 0, max_noise = 0;
  1247. s64 total, last_total = 0;
  1248. struct osnoise_sample s;
  1249. bool disable_preemption;
  1250. unsigned int threshold;
  1251. u64 runtime, stop_in;
  1252. u64 sum_noise = 0;
  1253. int hw_count = 0;
  1254. int ret = -1;
  1255. /*
  1256. * Disabling preemption is only required if IRQs are enabled,
  1257. * and the options is set on.
  1258. */
  1259. disable_preemption = !disable_irq && test_bit(OSN_PREEMPT_DISABLE, &osnoise_options);
  1260. /*
  1261. * Considers the current thread as the workload.
  1262. */
  1263. osn_var->pid = current->pid;
  1264. /*
  1265. * Save the current stats for the diff
  1266. */
  1267. save_osn_sample_stats(osn_var, &s);
  1268. /*
  1269. * if threshold is 0, use the default value of 1 us.
  1270. */
  1271. threshold = tracing_thresh ? : 1000;
  1272. /*
  1273. * Apply PREEMPT and IRQ disabled options.
  1274. */
  1275. if (disable_irq)
  1276. local_irq_disable();
  1277. if (disable_preemption)
  1278. preempt_disable();
  1279. /*
  1280. * Make sure NMIs see sampling first
  1281. */
  1282. osn_var->sampling = true;
  1283. barrier();
  1284. /*
  1285. * Transform the *_us config to nanoseconds to avoid the
  1286. * division on the main loop.
  1287. */
  1288. runtime = osnoise_data.sample_runtime * NSEC_PER_USEC;
  1289. stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC;
  1290. /*
  1291. * Start timestemp
  1292. */
  1293. start = time_get();
  1294. /*
  1295. * "previous" loop.
  1296. */
  1297. last_int_count = set_int_safe_time(osn_var, &last_sample);
  1298. do {
  1299. /*
  1300. * Get sample!
  1301. */
  1302. int_count = set_int_safe_time(osn_var, &sample);
  1303. noise = time_sub(sample, last_sample);
  1304. /*
  1305. * This shouldn't happen.
  1306. */
  1307. if (noise < 0) {
  1308. osnoise_taint("negative noise!");
  1309. goto out;
  1310. }
  1311. /*
  1312. * Sample runtime.
  1313. */
  1314. total = time_sub(sample, start);
  1315. /*
  1316. * Check for possible overflows.
  1317. */
  1318. if (total < last_total) {
  1319. osnoise_taint("total overflow!");
  1320. break;
  1321. }
  1322. last_total = total;
  1323. if (noise >= threshold) {
  1324. int interference = int_count - last_int_count;
  1325. if (noise > max_noise)
  1326. max_noise = noise;
  1327. if (!interference)
  1328. hw_count++;
  1329. sum_noise += noise;
  1330. trace_sample_threshold(last_sample, noise, interference);
  1331. if (osnoise_data.stop_tracing)
  1332. if (noise > stop_in)
  1333. osnoise_stop_tracing();
  1334. }
  1335. /*
  1336. * In some cases, notably when running on a nohz_full CPU with
  1337. * a stopped tick PREEMPT_RCU has no way to account for QSs.
  1338. * This will eventually cause unwarranted noise as PREEMPT_RCU
  1339. * will force preemption as the means of ending the current
  1340. * grace period. We avoid this problem by calling
  1341. * rcu_momentary_eqs(), which performs a zero duration
  1342. * EQS allowing PREEMPT_RCU to end the current grace period.
  1343. * This call shouldn't be wrapped inside an RCU critical
  1344. * section.
  1345. *
  1346. * Note that in non PREEMPT_RCU kernels QSs are handled through
  1347. * cond_resched()
  1348. */
  1349. if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
  1350. if (!disable_irq)
  1351. local_irq_disable();
  1352. rcu_momentary_eqs();
  1353. if (!disable_irq)
  1354. local_irq_enable();
  1355. }
  1356. /*
  1357. * For the non-preemptive kernel config: let threads runs, if
  1358. * they so wish, unless set not do to so.
  1359. */
  1360. if (!disable_irq && !disable_preemption)
  1361. cond_resched();
  1362. last_sample = sample;
  1363. last_int_count = int_count;
  1364. } while (total < runtime && !kthread_should_stop());
  1365. /*
  1366. * Finish the above in the view for interrupts.
  1367. */
  1368. barrier();
  1369. osn_var->sampling = false;
  1370. /*
  1371. * Make sure sampling data is no longer updated.
  1372. */
  1373. barrier();
  1374. /*
  1375. * Return to the preemptive state.
  1376. */
  1377. if (disable_preemption)
  1378. preempt_enable();
  1379. if (disable_irq)
  1380. local_irq_enable();
  1381. /*
  1382. * Save noise info.
  1383. */
  1384. s.noise = time_to_us(sum_noise);
  1385. s.runtime = time_to_us(total);
  1386. s.max_sample = time_to_us(max_noise);
  1387. s.hw_count = hw_count;
  1388. /* Save interference stats info */
  1389. diff_osn_sample_stats(osn_var, &s);
  1390. trace_osnoise_sample(&s);
  1391. notify_new_max_latency(max_noise);
  1392. if (osnoise_data.stop_tracing_total)
  1393. if (s.noise > osnoise_data.stop_tracing_total)
  1394. osnoise_stop_tracing();
  1395. return 0;
  1396. out:
  1397. return ret;
  1398. }
  1399. static struct cpumask osnoise_cpumask;
  1400. static struct cpumask save_cpumask;
  1401. static struct cpumask kthread_cpumask;
  1402. /*
  1403. * osnoise_sleep - sleep until the next period
  1404. */
  1405. static void osnoise_sleep(bool skip_period)
  1406. {
  1407. u64 interval;
  1408. ktime_t wake_time;
  1409. mutex_lock(&interface_lock);
  1410. if (skip_period)
  1411. interval = osnoise_data.sample_period;
  1412. else
  1413. interval = osnoise_data.sample_period - osnoise_data.sample_runtime;
  1414. mutex_unlock(&interface_lock);
  1415. /*
  1416. * differently from hwlat_detector, the osnoise tracer can run
  1417. * without a pause because preemption is on.
  1418. */
  1419. if (!interval) {
  1420. /* Let synchronize_rcu_tasks() make progress */
  1421. cond_resched_tasks_rcu_qs();
  1422. return;
  1423. }
  1424. wake_time = ktime_add_us(ktime_get(), interval);
  1425. __set_current_state(TASK_INTERRUPTIBLE);
  1426. while (schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS)) {
  1427. if (kthread_should_stop())
  1428. break;
  1429. }
  1430. }
  1431. /*
  1432. * osnoise_migration_pending - checks if the task needs to migrate
  1433. *
  1434. * osnoise/timerlat threads are per-cpu. If there is a pending request to
  1435. * migrate the thread away from the current CPU, something bad has happened.
  1436. * Play the good citizen and leave.
  1437. *
  1438. * Returns 0 if it is safe to continue, 1 otherwise.
  1439. */
  1440. static inline int osnoise_migration_pending(void)
  1441. {
  1442. if (!current->migration_pending)
  1443. return 0;
  1444. /*
  1445. * If migration is pending, there is a task waiting for the
  1446. * tracer to enable migration. The tracer does not allow migration,
  1447. * thus: taint and leave to unblock the blocked thread.
  1448. */
  1449. osnoise_taint("migration requested to osnoise threads, leaving.");
  1450. /*
  1451. * Unset this thread from the threads managed by the interface.
  1452. * The tracers are responsible for cleaning their env before
  1453. * exiting.
  1454. */
  1455. mutex_lock(&interface_lock);
  1456. this_cpu_osn_var()->kthread = NULL;
  1457. cpumask_clear_cpu(smp_processor_id(), &kthread_cpumask);
  1458. mutex_unlock(&interface_lock);
  1459. return 1;
  1460. }
  1461. /*
  1462. * osnoise_main - The osnoise detection kernel thread
  1463. *
  1464. * Calls run_osnoise() function to measure the osnoise for the configured runtime,
  1465. * every period.
  1466. */
  1467. static int osnoise_main(void *data)
  1468. {
  1469. unsigned long flags;
  1470. /*
  1471. * This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
  1472. * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
  1473. *
  1474. * To work around this limitation, disable migration and remove the
  1475. * flag.
  1476. */
  1477. migrate_disable();
  1478. raw_spin_lock_irqsave(&current->pi_lock, flags);
  1479. current->flags &= ~(PF_NO_SETAFFINITY);
  1480. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  1481. while (!kthread_should_stop()) {
  1482. if (osnoise_migration_pending())
  1483. break;
  1484. /* skip a period if tracing is off on all instances */
  1485. if (!osnoise_has_tracing_on()) {
  1486. osnoise_sleep(true);
  1487. continue;
  1488. }
  1489. run_osnoise();
  1490. osnoise_sleep(false);
  1491. }
  1492. migrate_enable();
  1493. return 0;
  1494. }
  1495. #ifdef CONFIG_TIMERLAT_TRACER
  1496. /*
  1497. * timerlat_irq - hrtimer handler for timerlat.
  1498. */
  1499. static enum hrtimer_restart timerlat_irq(struct hrtimer *timer)
  1500. {
  1501. struct osnoise_variables *osn_var = this_cpu_osn_var();
  1502. struct timerlat_variables *tlat;
  1503. struct timerlat_sample s;
  1504. u64 now;
  1505. u64 diff;
  1506. /*
  1507. * I am not sure if the timer was armed for this CPU. So, get
  1508. * the timerlat struct from the timer itself, not from this
  1509. * CPU.
  1510. */
  1511. tlat = container_of(timer, struct timerlat_variables, timer);
  1512. now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
  1513. /*
  1514. * Enable the osnoise: events for thread an softirq.
  1515. */
  1516. tlat->tracing_thread = true;
  1517. osn_var->thread.arrival_time = time_get();
  1518. /*
  1519. * A hardirq is running: the timer IRQ. It is for sure preempting
  1520. * a thread, and potentially preempting a softirq.
  1521. *
  1522. * At this point, it is not interesting to know the duration of the
  1523. * preempted thread (and maybe softirq), but how much time they will
  1524. * delay the beginning of the execution of the timer thread.
  1525. *
  1526. * To get the correct (net) delay added by the softirq, its delta_start
  1527. * is set as the IRQ one. In this way, at the return of the IRQ, the delta
  1528. * start of the sofitrq will be zeroed, accounting then only the time
  1529. * after that.
  1530. *
  1531. * The thread follows the same principle. However, if a softirq is
  1532. * running, the thread needs to receive the softirq delta_start. The
  1533. * reason being is that the softirq will be the last to be unfolded,
  1534. * resseting the thread delay to zero.
  1535. *
  1536. * The PREEMPT_RT is a special case, though. As softirqs run as threads
  1537. * on RT, moving the thread is enough.
  1538. */
  1539. if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) {
  1540. copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
  1541. &osn_var->softirq.delta_start);
  1542. copy_int_safe_time(osn_var, &osn_var->softirq.delta_start,
  1543. &osn_var->irq.delta_start);
  1544. } else {
  1545. copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
  1546. &osn_var->irq.delta_start);
  1547. }
  1548. /*
  1549. * Compute the current time with the expected time.
  1550. */
  1551. diff = now - tlat->abs_period;
  1552. tlat->count++;
  1553. s.seqnum = tlat->count;
  1554. s.timer_latency = diff;
  1555. s.context = IRQ_CONTEXT;
  1556. trace_timerlat_sample(&s);
  1557. if (osnoise_data.stop_tracing) {
  1558. if (time_to_us(diff) >= osnoise_data.stop_tracing) {
  1559. /*
  1560. * At this point, if stop_tracing is set and <= print_stack,
  1561. * print_stack is set and would be printed in the thread handler.
  1562. *
  1563. * Thus, print the stack trace as it is helpful to define the
  1564. * root cause of an IRQ latency.
  1565. */
  1566. if (osnoise_data.stop_tracing <= osnoise_data.print_stack) {
  1567. timerlat_save_stack(0);
  1568. timerlat_dump_stack(time_to_us(diff));
  1569. }
  1570. osnoise_stop_tracing();
  1571. notify_new_max_latency(diff);
  1572. wake_up_process(tlat->kthread);
  1573. return HRTIMER_NORESTART;
  1574. }
  1575. }
  1576. wake_up_process(tlat->kthread);
  1577. if (osnoise_data.print_stack)
  1578. timerlat_save_stack(0);
  1579. return HRTIMER_NORESTART;
  1580. }
  1581. /*
  1582. * wait_next_period - Wait for the next period for timerlat
  1583. */
  1584. static int wait_next_period(struct timerlat_variables *tlat)
  1585. {
  1586. ktime_t next_abs_period, now;
  1587. u64 rel_period = osnoise_data.timerlat_period * 1000;
  1588. now = hrtimer_cb_get_time(&tlat->timer);
  1589. next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
  1590. /*
  1591. * Save the next abs_period.
  1592. */
  1593. tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
  1594. /*
  1595. * If the new abs_period is in the past, skip the activation.
  1596. */
  1597. while (ktime_compare(now, next_abs_period) > 0) {
  1598. next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
  1599. tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
  1600. }
  1601. set_current_state(TASK_INTERRUPTIBLE);
  1602. hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD);
  1603. schedule();
  1604. return 1;
  1605. }
  1606. /*
  1607. * timerlat_main- Timerlat main
  1608. */
  1609. static int timerlat_main(void *data)
  1610. {
  1611. struct osnoise_variables *osn_var = this_cpu_osn_var();
  1612. struct timerlat_variables *tlat = this_cpu_tmr_var();
  1613. struct timerlat_sample s;
  1614. struct sched_param sp;
  1615. unsigned long flags;
  1616. u64 now, diff;
  1617. /*
  1618. * Make the thread RT, that is how cyclictest is usually used.
  1619. */
  1620. sp.sched_priority = DEFAULT_TIMERLAT_PRIO;
  1621. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1622. /*
  1623. * This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
  1624. * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
  1625. *
  1626. * To work around this limitation, disable migration and remove the
  1627. * flag.
  1628. */
  1629. migrate_disable();
  1630. raw_spin_lock_irqsave(&current->pi_lock, flags);
  1631. current->flags &= ~(PF_NO_SETAFFINITY);
  1632. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  1633. tlat->count = 0;
  1634. tlat->tracing_thread = false;
  1635. hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
  1636. tlat->timer.function = timerlat_irq;
  1637. tlat->kthread = current;
  1638. osn_var->pid = current->pid;
  1639. /*
  1640. * Anotate the arrival time.
  1641. */
  1642. tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
  1643. wait_next_period(tlat);
  1644. osn_var->sampling = 1;
  1645. while (!kthread_should_stop()) {
  1646. now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
  1647. diff = now - tlat->abs_period;
  1648. s.seqnum = tlat->count;
  1649. s.timer_latency = diff;
  1650. s.context = THREAD_CONTEXT;
  1651. trace_timerlat_sample(&s);
  1652. notify_new_max_latency(diff);
  1653. timerlat_dump_stack(time_to_us(diff));
  1654. tlat->tracing_thread = false;
  1655. if (osnoise_data.stop_tracing_total)
  1656. if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
  1657. osnoise_stop_tracing();
  1658. if (osnoise_migration_pending())
  1659. break;
  1660. wait_next_period(tlat);
  1661. }
  1662. hrtimer_cancel(&tlat->timer);
  1663. migrate_enable();
  1664. return 0;
  1665. }
  1666. #else /* CONFIG_TIMERLAT_TRACER */
  1667. static int timerlat_main(void *data)
  1668. {
  1669. return 0;
  1670. }
  1671. #endif /* CONFIG_TIMERLAT_TRACER */
  1672. /*
  1673. * stop_kthread - stop a workload thread
  1674. */
  1675. static void stop_kthread(unsigned int cpu)
  1676. {
  1677. struct task_struct *kthread;
  1678. kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
  1679. if (kthread) {
  1680. if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask) &&
  1681. !WARN_ON(!test_bit(OSN_WORKLOAD, &osnoise_options))) {
  1682. kthread_stop(kthread);
  1683. } else if (!WARN_ON(test_bit(OSN_WORKLOAD, &osnoise_options))) {
  1684. /*
  1685. * This is a user thread waiting on the timerlat_fd. We need
  1686. * to close all users, and the best way to guarantee this is
  1687. * by killing the thread. NOTE: this is a purpose specific file.
  1688. */
  1689. kill_pid(kthread->thread_pid, SIGKILL, 1);
  1690. put_task_struct(kthread);
  1691. }
  1692. } else {
  1693. /* if no workload, just return */
  1694. if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
  1695. /*
  1696. * This is set in the osnoise tracer case.
  1697. */
  1698. per_cpu(per_cpu_osnoise_var, cpu).sampling = false;
  1699. barrier();
  1700. }
  1701. }
  1702. }
  1703. /*
  1704. * stop_per_cpu_kthread - Stop per-cpu threads
  1705. *
  1706. * Stop the osnoise sampling htread. Use this on unload and at system
  1707. * shutdown.
  1708. */
  1709. static void stop_per_cpu_kthreads(void)
  1710. {
  1711. int cpu;
  1712. cpus_read_lock();
  1713. for_each_online_cpu(cpu)
  1714. stop_kthread(cpu);
  1715. cpus_read_unlock();
  1716. }
  1717. /*
  1718. * start_kthread - Start a workload tread
  1719. */
  1720. static int start_kthread(unsigned int cpu)
  1721. {
  1722. struct task_struct *kthread;
  1723. void *main = osnoise_main;
  1724. char comm[24];
  1725. /* Do not start a new thread if it is already running */
  1726. if (per_cpu(per_cpu_osnoise_var, cpu).kthread)
  1727. return 0;
  1728. if (timerlat_enabled()) {
  1729. snprintf(comm, 24, "timerlat/%d", cpu);
  1730. main = timerlat_main;
  1731. } else {
  1732. /* if no workload, just return */
  1733. if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
  1734. per_cpu(per_cpu_osnoise_var, cpu).sampling = true;
  1735. barrier();
  1736. return 0;
  1737. }
  1738. snprintf(comm, 24, "osnoise/%d", cpu);
  1739. }
  1740. kthread = kthread_run_on_cpu(main, NULL, cpu, comm);
  1741. if (IS_ERR(kthread)) {
  1742. pr_err(BANNER "could not start sampling thread\n");
  1743. return -ENOMEM;
  1744. }
  1745. per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
  1746. cpumask_set_cpu(cpu, &kthread_cpumask);
  1747. return 0;
  1748. }
  1749. /*
  1750. * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads
  1751. *
  1752. * This starts the kernel thread that will look for osnoise on many
  1753. * cpus.
  1754. */
  1755. static int start_per_cpu_kthreads(void)
  1756. {
  1757. struct cpumask *current_mask = &save_cpumask;
  1758. int retval = 0;
  1759. int cpu;
  1760. if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
  1761. if (timerlat_enabled())
  1762. return 0;
  1763. }
  1764. cpus_read_lock();
  1765. /*
  1766. * Run only on online CPUs in which osnoise is allowed to run.
  1767. */
  1768. cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask);
  1769. for_each_possible_cpu(cpu) {
  1770. if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask)) {
  1771. struct task_struct *kthread;
  1772. kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
  1773. if (!WARN_ON(!kthread))
  1774. kthread_stop(kthread);
  1775. }
  1776. }
  1777. for_each_cpu(cpu, current_mask) {
  1778. retval = start_kthread(cpu);
  1779. if (retval) {
  1780. cpus_read_unlock();
  1781. stop_per_cpu_kthreads();
  1782. return retval;
  1783. }
  1784. }
  1785. cpus_read_unlock();
  1786. return retval;
  1787. }
  1788. #ifdef CONFIG_HOTPLUG_CPU
  1789. static void osnoise_hotplug_workfn(struct work_struct *dummy)
  1790. {
  1791. unsigned int cpu = smp_processor_id();
  1792. mutex_lock(&trace_types_lock);
  1793. if (!osnoise_has_registered_instances())
  1794. goto out_unlock_trace;
  1795. mutex_lock(&interface_lock);
  1796. cpus_read_lock();
  1797. if (!cpu_online(cpu))
  1798. goto out_unlock;
  1799. if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
  1800. goto out_unlock;
  1801. start_kthread(cpu);
  1802. out_unlock:
  1803. cpus_read_unlock();
  1804. mutex_unlock(&interface_lock);
  1805. out_unlock_trace:
  1806. mutex_unlock(&trace_types_lock);
  1807. }
  1808. static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
  1809. /*
  1810. * osnoise_cpu_init - CPU hotplug online callback function
  1811. */
  1812. static int osnoise_cpu_init(unsigned int cpu)
  1813. {
  1814. schedule_work_on(cpu, &osnoise_hotplug_work);
  1815. return 0;
  1816. }
  1817. /*
  1818. * osnoise_cpu_die - CPU hotplug offline callback function
  1819. */
  1820. static int osnoise_cpu_die(unsigned int cpu)
  1821. {
  1822. stop_kthread(cpu);
  1823. return 0;
  1824. }
  1825. static void osnoise_init_hotplug_support(void)
  1826. {
  1827. int ret;
  1828. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online",
  1829. osnoise_cpu_init, osnoise_cpu_die);
  1830. if (ret < 0)
  1831. pr_warn(BANNER "Error to init cpu hotplug support\n");
  1832. return;
  1833. }
  1834. #else /* CONFIG_HOTPLUG_CPU */
  1835. static void osnoise_init_hotplug_support(void)
  1836. {
  1837. return;
  1838. }
  1839. #endif /* CONFIG_HOTPLUG_CPU */
  1840. /*
  1841. * seq file functions for the osnoise/options file.
  1842. */
  1843. static void *s_options_start(struct seq_file *s, loff_t *pos)
  1844. {
  1845. int option = *pos;
  1846. mutex_lock(&interface_lock);
  1847. if (option >= OSN_MAX)
  1848. return NULL;
  1849. return pos;
  1850. }
  1851. static void *s_options_next(struct seq_file *s, void *v, loff_t *pos)
  1852. {
  1853. int option = ++(*pos);
  1854. if (option >= OSN_MAX)
  1855. return NULL;
  1856. return pos;
  1857. }
  1858. static int s_options_show(struct seq_file *s, void *v)
  1859. {
  1860. loff_t *pos = v;
  1861. int option = *pos;
  1862. if (option == OSN_DEFAULTS) {
  1863. if (osnoise_options == OSN_DEFAULT_OPTIONS)
  1864. seq_printf(s, "%s", osnoise_options_str[option]);
  1865. else
  1866. seq_printf(s, "NO_%s", osnoise_options_str[option]);
  1867. goto out;
  1868. }
  1869. if (test_bit(option, &osnoise_options))
  1870. seq_printf(s, "%s", osnoise_options_str[option]);
  1871. else
  1872. seq_printf(s, "NO_%s", osnoise_options_str[option]);
  1873. out:
  1874. if (option != OSN_MAX)
  1875. seq_puts(s, " ");
  1876. return 0;
  1877. }
  1878. static void s_options_stop(struct seq_file *s, void *v)
  1879. {
  1880. seq_puts(s, "\n");
  1881. mutex_unlock(&interface_lock);
  1882. }
  1883. static const struct seq_operations osnoise_options_seq_ops = {
  1884. .start = s_options_start,
  1885. .next = s_options_next,
  1886. .show = s_options_show,
  1887. .stop = s_options_stop
  1888. };
  1889. static int osnoise_options_open(struct inode *inode, struct file *file)
  1890. {
  1891. return seq_open(file, &osnoise_options_seq_ops);
  1892. };
  1893. /**
  1894. * osnoise_options_write - Write function for "options" entry
  1895. * @filp: The active open file structure
  1896. * @ubuf: The user buffer that contains the value to write
  1897. * @cnt: The maximum number of bytes to write to "file"
  1898. * @ppos: The current position in @file
  1899. *
  1900. * Writing the option name sets the option, writing the "NO_"
  1901. * prefix in front of the option name disables it.
  1902. *
  1903. * Writing "DEFAULTS" resets the option values to the default ones.
  1904. */
  1905. static ssize_t osnoise_options_write(struct file *filp, const char __user *ubuf,
  1906. size_t cnt, loff_t *ppos)
  1907. {
  1908. int running, option, enable, retval;
  1909. char buf[256], *option_str;
  1910. if (cnt >= 256)
  1911. return -EINVAL;
  1912. if (copy_from_user(buf, ubuf, cnt))
  1913. return -EFAULT;
  1914. buf[cnt] = 0;
  1915. if (strncmp(buf, "NO_", 3)) {
  1916. option_str = strstrip(buf);
  1917. enable = true;
  1918. } else {
  1919. option_str = strstrip(&buf[3]);
  1920. enable = false;
  1921. }
  1922. option = match_string(osnoise_options_str, OSN_MAX, option_str);
  1923. if (option < 0)
  1924. return -EINVAL;
  1925. /*
  1926. * trace_types_lock is taken to avoid concurrency on start/stop.
  1927. */
  1928. mutex_lock(&trace_types_lock);
  1929. running = osnoise_has_registered_instances();
  1930. if (running)
  1931. stop_per_cpu_kthreads();
  1932. mutex_lock(&interface_lock);
  1933. /*
  1934. * avoid CPU hotplug operations that might read options.
  1935. */
  1936. cpus_read_lock();
  1937. retval = cnt;
  1938. if (enable) {
  1939. if (option == OSN_DEFAULTS)
  1940. osnoise_options = OSN_DEFAULT_OPTIONS;
  1941. else
  1942. set_bit(option, &osnoise_options);
  1943. } else {
  1944. if (option == OSN_DEFAULTS)
  1945. retval = -EINVAL;
  1946. else
  1947. clear_bit(option, &osnoise_options);
  1948. }
  1949. cpus_read_unlock();
  1950. mutex_unlock(&interface_lock);
  1951. if (running)
  1952. start_per_cpu_kthreads();
  1953. mutex_unlock(&trace_types_lock);
  1954. return retval;
  1955. }
  1956. /*
  1957. * osnoise_cpus_read - Read function for reading the "cpus" file
  1958. * @filp: The active open file structure
  1959. * @ubuf: The userspace provided buffer to read value into
  1960. * @cnt: The maximum number of bytes to read
  1961. * @ppos: The current "file" position
  1962. *
  1963. * Prints the "cpus" output into the user-provided buffer.
  1964. */
  1965. static ssize_t
  1966. osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count,
  1967. loff_t *ppos)
  1968. {
  1969. char *mask_str;
  1970. int len;
  1971. mutex_lock(&interface_lock);
  1972. len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1;
  1973. mask_str = kmalloc(len, GFP_KERNEL);
  1974. if (!mask_str) {
  1975. count = -ENOMEM;
  1976. goto out_unlock;
  1977. }
  1978. len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask));
  1979. if (len >= count) {
  1980. count = -EINVAL;
  1981. goto out_free;
  1982. }
  1983. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
  1984. out_free:
  1985. kfree(mask_str);
  1986. out_unlock:
  1987. mutex_unlock(&interface_lock);
  1988. return count;
  1989. }
  1990. /*
  1991. * osnoise_cpus_write - Write function for "cpus" entry
  1992. * @filp: The active open file structure
  1993. * @ubuf: The user buffer that contains the value to write
  1994. * @cnt: The maximum number of bytes to write to "file"
  1995. * @ppos: The current position in @file
  1996. *
  1997. * This function provides a write implementation for the "cpus"
  1998. * interface to the osnoise trace. By default, it lists all CPUs,
  1999. * in this way, allowing osnoise threads to run on any online CPU
  2000. * of the system. It serves to restrict the execution of osnoise to the
  2001. * set of CPUs writing via this interface. Why not use "tracing_cpumask"?
  2002. * Because the user might be interested in tracing what is running on
  2003. * other CPUs. For instance, one might run osnoise in one HT CPU
  2004. * while observing what is running on the sibling HT CPU.
  2005. */
  2006. static ssize_t
  2007. osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count,
  2008. loff_t *ppos)
  2009. {
  2010. cpumask_var_t osnoise_cpumask_new;
  2011. int running, err;
  2012. char buf[256];
  2013. if (count >= 256)
  2014. return -EINVAL;
  2015. if (copy_from_user(buf, ubuf, count))
  2016. return -EFAULT;
  2017. if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL))
  2018. return -ENOMEM;
  2019. err = cpulist_parse(buf, osnoise_cpumask_new);
  2020. if (err)
  2021. goto err_free;
  2022. /*
  2023. * trace_types_lock is taken to avoid concurrency on start/stop.
  2024. */
  2025. mutex_lock(&trace_types_lock);
  2026. running = osnoise_has_registered_instances();
  2027. if (running)
  2028. stop_per_cpu_kthreads();
  2029. mutex_lock(&interface_lock);
  2030. /*
  2031. * osnoise_cpumask is read by CPU hotplug operations.
  2032. */
  2033. cpus_read_lock();
  2034. cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new);
  2035. cpus_read_unlock();
  2036. mutex_unlock(&interface_lock);
  2037. if (running)
  2038. start_per_cpu_kthreads();
  2039. mutex_unlock(&trace_types_lock);
  2040. free_cpumask_var(osnoise_cpumask_new);
  2041. return count;
  2042. err_free:
  2043. free_cpumask_var(osnoise_cpumask_new);
  2044. return err;
  2045. }
  2046. #ifdef CONFIG_TIMERLAT_TRACER
  2047. static int timerlat_fd_open(struct inode *inode, struct file *file)
  2048. {
  2049. struct osnoise_variables *osn_var;
  2050. struct timerlat_variables *tlat;
  2051. long cpu = (long) inode->i_cdev;
  2052. mutex_lock(&interface_lock);
  2053. /*
  2054. * This file is accessible only if timerlat is enabled, and
  2055. * NO_OSNOISE_WORKLOAD is set.
  2056. */
  2057. if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options)) {
  2058. mutex_unlock(&interface_lock);
  2059. return -EINVAL;
  2060. }
  2061. migrate_disable();
  2062. osn_var = this_cpu_osn_var();
  2063. /*
  2064. * The osn_var->pid holds the single access to this file.
  2065. */
  2066. if (osn_var->pid) {
  2067. mutex_unlock(&interface_lock);
  2068. migrate_enable();
  2069. return -EBUSY;
  2070. }
  2071. /*
  2072. * timerlat tracer is a per-cpu tracer. Check if the user-space too
  2073. * is pinned to a single CPU. The tracer laters monitor if the task
  2074. * migrates and then disables tracer if it does. However, it is
  2075. * worth doing this basic acceptance test to avoid obviusly wrong
  2076. * setup.
  2077. */
  2078. if (current->nr_cpus_allowed > 1 || cpu != smp_processor_id()) {
  2079. mutex_unlock(&interface_lock);
  2080. migrate_enable();
  2081. return -EPERM;
  2082. }
  2083. /*
  2084. * From now on, it is good to go.
  2085. */
  2086. file->private_data = inode->i_cdev;
  2087. get_task_struct(current);
  2088. osn_var->kthread = current;
  2089. osn_var->pid = current->pid;
  2090. /*
  2091. * Setup is done.
  2092. */
  2093. mutex_unlock(&interface_lock);
  2094. tlat = this_cpu_tmr_var();
  2095. tlat->count = 0;
  2096. hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
  2097. tlat->timer.function = timerlat_irq;
  2098. migrate_enable();
  2099. return 0;
  2100. };
  2101. /*
  2102. * timerlat_fd_read - Read function for "timerlat_fd" file
  2103. * @file: The active open file structure
  2104. * @ubuf: The userspace provided buffer to read value into
  2105. * @cnt: The maximum number of bytes to read
  2106. * @ppos: The current "file" position
  2107. *
  2108. * Prints 1 on timerlat, the number of interferences on osnoise, -1 on error.
  2109. */
  2110. static ssize_t
  2111. timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
  2112. loff_t *ppos)
  2113. {
  2114. long cpu = (long) file->private_data;
  2115. struct osnoise_variables *osn_var;
  2116. struct timerlat_variables *tlat;
  2117. struct timerlat_sample s;
  2118. s64 diff;
  2119. u64 now;
  2120. migrate_disable();
  2121. tlat = this_cpu_tmr_var();
  2122. /*
  2123. * While in user-space, the thread is migratable. There is nothing
  2124. * we can do about it.
  2125. * So, if the thread is running on another CPU, stop the machinery.
  2126. */
  2127. if (cpu == smp_processor_id()) {
  2128. if (tlat->uthread_migrate) {
  2129. migrate_enable();
  2130. return -EINVAL;
  2131. }
  2132. } else {
  2133. per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
  2134. osnoise_taint("timerlat user thread migrate\n");
  2135. osnoise_stop_tracing();
  2136. migrate_enable();
  2137. return -EINVAL;
  2138. }
  2139. osn_var = this_cpu_osn_var();
  2140. /*
  2141. * The timerlat in user-space runs in a different order:
  2142. * the read() starts from the execution of the previous occurrence,
  2143. * sleeping for the next occurrence.
  2144. *
  2145. * So, skip if we are entering on read() before the first wakeup
  2146. * from timerlat IRQ:
  2147. */
  2148. if (likely(osn_var->sampling)) {
  2149. now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
  2150. diff = now - tlat->abs_period;
  2151. /*
  2152. * it was not a timer firing, but some other signal?
  2153. */
  2154. if (diff < 0)
  2155. goto out;
  2156. s.seqnum = tlat->count;
  2157. s.timer_latency = diff;
  2158. s.context = THREAD_URET;
  2159. trace_timerlat_sample(&s);
  2160. notify_new_max_latency(diff);
  2161. tlat->tracing_thread = false;
  2162. if (osnoise_data.stop_tracing_total)
  2163. if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
  2164. osnoise_stop_tracing();
  2165. } else {
  2166. tlat->tracing_thread = false;
  2167. tlat->kthread = current;
  2168. /* Annotate now to drift new period */
  2169. tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
  2170. osn_var->sampling = 1;
  2171. }
  2172. /* wait for the next period */
  2173. wait_next_period(tlat);
  2174. /* This is the wakeup from this cycle */
  2175. now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
  2176. diff = now - tlat->abs_period;
  2177. /*
  2178. * it was not a timer firing, but some other signal?
  2179. */
  2180. if (diff < 0)
  2181. goto out;
  2182. s.seqnum = tlat->count;
  2183. s.timer_latency = diff;
  2184. s.context = THREAD_CONTEXT;
  2185. trace_timerlat_sample(&s);
  2186. if (osnoise_data.stop_tracing_total) {
  2187. if (time_to_us(diff) >= osnoise_data.stop_tracing_total) {
  2188. timerlat_dump_stack(time_to_us(diff));
  2189. notify_new_max_latency(diff);
  2190. osnoise_stop_tracing();
  2191. }
  2192. }
  2193. out:
  2194. migrate_enable();
  2195. return 0;
  2196. }
  2197. static int timerlat_fd_release(struct inode *inode, struct file *file)
  2198. {
  2199. struct osnoise_variables *osn_var;
  2200. struct timerlat_variables *tlat_var;
  2201. long cpu = (long) file->private_data;
  2202. migrate_disable();
  2203. mutex_lock(&interface_lock);
  2204. osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
  2205. tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
  2206. if (tlat_var->kthread)
  2207. hrtimer_cancel(&tlat_var->timer);
  2208. memset(tlat_var, 0, sizeof(*tlat_var));
  2209. osn_var->sampling = 0;
  2210. osn_var->pid = 0;
  2211. /*
  2212. * We are leaving, not being stopped... see stop_kthread();
  2213. */
  2214. if (osn_var->kthread) {
  2215. put_task_struct(osn_var->kthread);
  2216. osn_var->kthread = NULL;
  2217. }
  2218. mutex_unlock(&interface_lock);
  2219. migrate_enable();
  2220. return 0;
  2221. }
  2222. #endif
  2223. /*
  2224. * osnoise/runtime_us: cannot be greater than the period.
  2225. */
  2226. static struct trace_min_max_param osnoise_runtime = {
  2227. .lock = &interface_lock,
  2228. .val = &osnoise_data.sample_runtime,
  2229. .max = &osnoise_data.sample_period,
  2230. .min = NULL,
  2231. };
  2232. /*
  2233. * osnoise/period_us: cannot be smaller than the runtime.
  2234. */
  2235. static struct trace_min_max_param osnoise_period = {
  2236. .lock = &interface_lock,
  2237. .val = &osnoise_data.sample_period,
  2238. .max = NULL,
  2239. .min = &osnoise_data.sample_runtime,
  2240. };
  2241. /*
  2242. * osnoise/stop_tracing_us: no limit.
  2243. */
  2244. static struct trace_min_max_param osnoise_stop_tracing_in = {
  2245. .lock = &interface_lock,
  2246. .val = &osnoise_data.stop_tracing,
  2247. .max = NULL,
  2248. .min = NULL,
  2249. };
  2250. /*
  2251. * osnoise/stop_tracing_total_us: no limit.
  2252. */
  2253. static struct trace_min_max_param osnoise_stop_tracing_total = {
  2254. .lock = &interface_lock,
  2255. .val = &osnoise_data.stop_tracing_total,
  2256. .max = NULL,
  2257. .min = NULL,
  2258. };
  2259. #ifdef CONFIG_TIMERLAT_TRACER
  2260. /*
  2261. * osnoise/print_stack: print the stacktrace of the IRQ handler if the total
  2262. * latency is higher than val.
  2263. */
  2264. static struct trace_min_max_param osnoise_print_stack = {
  2265. .lock = &interface_lock,
  2266. .val = &osnoise_data.print_stack,
  2267. .max = NULL,
  2268. .min = NULL,
  2269. };
  2270. /*
  2271. * osnoise/timerlat_period: min 100 us, max 1 s
  2272. */
  2273. static u64 timerlat_min_period = 100;
  2274. static u64 timerlat_max_period = 1000000;
  2275. static struct trace_min_max_param timerlat_period = {
  2276. .lock = &interface_lock,
  2277. .val = &osnoise_data.timerlat_period,
  2278. .max = &timerlat_max_period,
  2279. .min = &timerlat_min_period,
  2280. };
  2281. static const struct file_operations timerlat_fd_fops = {
  2282. .open = timerlat_fd_open,
  2283. .read = timerlat_fd_read,
  2284. .release = timerlat_fd_release,
  2285. .llseek = generic_file_llseek,
  2286. };
  2287. #endif
  2288. static const struct file_operations cpus_fops = {
  2289. .open = tracing_open_generic,
  2290. .read = osnoise_cpus_read,
  2291. .write = osnoise_cpus_write,
  2292. .llseek = generic_file_llseek,
  2293. };
  2294. static const struct file_operations osnoise_options_fops = {
  2295. .open = osnoise_options_open,
  2296. .read = seq_read,
  2297. .llseek = seq_lseek,
  2298. .release = seq_release,
  2299. .write = osnoise_options_write
  2300. };
  2301. #ifdef CONFIG_TIMERLAT_TRACER
  2302. #ifdef CONFIG_STACKTRACE
  2303. static int init_timerlat_stack_tracefs(struct dentry *top_dir)
  2304. {
  2305. struct dentry *tmp;
  2306. tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir,
  2307. &osnoise_print_stack, &trace_min_max_fops);
  2308. if (!tmp)
  2309. return -ENOMEM;
  2310. return 0;
  2311. }
  2312. #else /* CONFIG_STACKTRACE */
  2313. static int init_timerlat_stack_tracefs(struct dentry *top_dir)
  2314. {
  2315. return 0;
  2316. }
  2317. #endif /* CONFIG_STACKTRACE */
  2318. static int osnoise_create_cpu_timerlat_fd(struct dentry *top_dir)
  2319. {
  2320. struct dentry *timerlat_fd;
  2321. struct dentry *per_cpu;
  2322. struct dentry *cpu_dir;
  2323. char cpu_str[30]; /* see trace.c: tracing_init_tracefs_percpu() */
  2324. long cpu;
  2325. /*
  2326. * Why not using tracing instance per_cpu/ dir?
  2327. *
  2328. * Because osnoise/timerlat have a single workload, having
  2329. * multiple files like these are wast of memory.
  2330. */
  2331. per_cpu = tracefs_create_dir("per_cpu", top_dir);
  2332. if (!per_cpu)
  2333. return -ENOMEM;
  2334. for_each_possible_cpu(cpu) {
  2335. snprintf(cpu_str, 30, "cpu%ld", cpu);
  2336. cpu_dir = tracefs_create_dir(cpu_str, per_cpu);
  2337. if (!cpu_dir)
  2338. goto out_clean;
  2339. timerlat_fd = trace_create_file("timerlat_fd", TRACE_MODE_READ,
  2340. cpu_dir, NULL, &timerlat_fd_fops);
  2341. if (!timerlat_fd)
  2342. goto out_clean;
  2343. /* Record the CPU */
  2344. d_inode(timerlat_fd)->i_cdev = (void *)(cpu);
  2345. }
  2346. return 0;
  2347. out_clean:
  2348. tracefs_remove(per_cpu);
  2349. return -ENOMEM;
  2350. }
  2351. /*
  2352. * init_timerlat_tracefs - A function to initialize the timerlat interface files
  2353. */
  2354. static int init_timerlat_tracefs(struct dentry *top_dir)
  2355. {
  2356. struct dentry *tmp;
  2357. int retval;
  2358. tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir,
  2359. &timerlat_period, &trace_min_max_fops);
  2360. if (!tmp)
  2361. return -ENOMEM;
  2362. retval = osnoise_create_cpu_timerlat_fd(top_dir);
  2363. if (retval)
  2364. return retval;
  2365. return init_timerlat_stack_tracefs(top_dir);
  2366. }
  2367. #else /* CONFIG_TIMERLAT_TRACER */
  2368. static int init_timerlat_tracefs(struct dentry *top_dir)
  2369. {
  2370. return 0;
  2371. }
  2372. #endif /* CONFIG_TIMERLAT_TRACER */
  2373. /*
  2374. * init_tracefs - A function to initialize the tracefs interface files
  2375. *
  2376. * This function creates entries in tracefs for "osnoise" and "timerlat".
  2377. * It creates these directories in the tracing directory, and within that
  2378. * directory the use can change and view the configs.
  2379. */
  2380. static int init_tracefs(void)
  2381. {
  2382. struct dentry *top_dir;
  2383. struct dentry *tmp;
  2384. int ret;
  2385. ret = tracing_init_dentry();
  2386. if (ret)
  2387. return -ENOMEM;
  2388. top_dir = tracefs_create_dir("osnoise", NULL);
  2389. if (!top_dir)
  2390. return 0;
  2391. tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir,
  2392. &osnoise_period, &trace_min_max_fops);
  2393. if (!tmp)
  2394. goto err;
  2395. tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir,
  2396. &osnoise_runtime, &trace_min_max_fops);
  2397. if (!tmp)
  2398. goto err;
  2399. tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir,
  2400. &osnoise_stop_tracing_in, &trace_min_max_fops);
  2401. if (!tmp)
  2402. goto err;
  2403. tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir,
  2404. &osnoise_stop_tracing_total, &trace_min_max_fops);
  2405. if (!tmp)
  2406. goto err;
  2407. tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops);
  2408. if (!tmp)
  2409. goto err;
  2410. tmp = trace_create_file("options", TRACE_MODE_WRITE, top_dir, NULL,
  2411. &osnoise_options_fops);
  2412. if (!tmp)
  2413. goto err;
  2414. ret = init_timerlat_tracefs(top_dir);
  2415. if (ret)
  2416. goto err;
  2417. return 0;
  2418. err:
  2419. tracefs_remove(top_dir);
  2420. return -ENOMEM;
  2421. }
  2422. static int osnoise_hook_events(void)
  2423. {
  2424. int retval;
  2425. /*
  2426. * Trace is already hooked, we are re-enabling from
  2427. * a stop_tracing_*.
  2428. */
  2429. if (trace_osnoise_callback_enabled)
  2430. return 0;
  2431. retval = hook_irq_events();
  2432. if (retval)
  2433. return -EINVAL;
  2434. retval = hook_softirq_events();
  2435. if (retval)
  2436. goto out_unhook_irq;
  2437. retval = hook_thread_events();
  2438. /*
  2439. * All fine!
  2440. */
  2441. if (!retval)
  2442. return 0;
  2443. unhook_softirq_events();
  2444. out_unhook_irq:
  2445. unhook_irq_events();
  2446. return -EINVAL;
  2447. }
  2448. static void osnoise_unhook_events(void)
  2449. {
  2450. unhook_thread_events();
  2451. unhook_softirq_events();
  2452. unhook_irq_events();
  2453. }
  2454. /*
  2455. * osnoise_workload_start - start the workload and hook to events
  2456. */
  2457. static int osnoise_workload_start(void)
  2458. {
  2459. int retval;
  2460. /*
  2461. * Instances need to be registered after calling workload
  2462. * start. Hence, if there is already an instance, the
  2463. * workload was already registered. Otherwise, this
  2464. * code is on the way to register the first instance,
  2465. * and the workload will start.
  2466. */
  2467. if (osnoise_has_registered_instances())
  2468. return 0;
  2469. osn_var_reset_all();
  2470. retval = osnoise_hook_events();
  2471. if (retval)
  2472. return retval;
  2473. /*
  2474. * Make sure that ftrace_nmi_enter/exit() see reset values
  2475. * before enabling trace_osnoise_callback_enabled.
  2476. */
  2477. barrier();
  2478. trace_osnoise_callback_enabled = true;
  2479. retval = start_per_cpu_kthreads();
  2480. if (retval) {
  2481. trace_osnoise_callback_enabled = false;
  2482. /*
  2483. * Make sure that ftrace_nmi_enter/exit() see
  2484. * trace_osnoise_callback_enabled as false before continuing.
  2485. */
  2486. barrier();
  2487. osnoise_unhook_events();
  2488. return retval;
  2489. }
  2490. return 0;
  2491. }
  2492. /*
  2493. * osnoise_workload_stop - stop the workload and unhook the events
  2494. */
  2495. static void osnoise_workload_stop(void)
  2496. {
  2497. /*
  2498. * Instances need to be unregistered before calling
  2499. * stop. Hence, if there is a registered instance, more
  2500. * than one instance is running, and the workload will not
  2501. * yet stop. Otherwise, this code is on the way to disable
  2502. * the last instance, and the workload can stop.
  2503. */
  2504. if (osnoise_has_registered_instances())
  2505. return;
  2506. /*
  2507. * If callbacks were already disabled in a previous stop
  2508. * call, there is no need to disable then again.
  2509. *
  2510. * For instance, this happens when tracing is stopped via:
  2511. * echo 0 > tracing_on
  2512. * echo nop > current_tracer.
  2513. */
  2514. if (!trace_osnoise_callback_enabled)
  2515. return;
  2516. trace_osnoise_callback_enabled = false;
  2517. /*
  2518. * Make sure that ftrace_nmi_enter/exit() see
  2519. * trace_osnoise_callback_enabled as false before continuing.
  2520. */
  2521. barrier();
  2522. stop_per_cpu_kthreads();
  2523. osnoise_unhook_events();
  2524. }
  2525. static void osnoise_tracer_start(struct trace_array *tr)
  2526. {
  2527. int retval;
  2528. /*
  2529. * If the instance is already registered, there is no need to
  2530. * register it again.
  2531. */
  2532. if (osnoise_instance_registered(tr))
  2533. return;
  2534. retval = osnoise_workload_start();
  2535. if (retval)
  2536. pr_err(BANNER "Error starting osnoise tracer\n");
  2537. osnoise_register_instance(tr);
  2538. }
  2539. static void osnoise_tracer_stop(struct trace_array *tr)
  2540. {
  2541. osnoise_unregister_instance(tr);
  2542. osnoise_workload_stop();
  2543. }
  2544. static int osnoise_tracer_init(struct trace_array *tr)
  2545. {
  2546. /*
  2547. * Only allow osnoise tracer if timerlat tracer is not running
  2548. * already.
  2549. */
  2550. if (timerlat_enabled())
  2551. return -EBUSY;
  2552. tr->max_latency = 0;
  2553. osnoise_tracer_start(tr);
  2554. return 0;
  2555. }
  2556. static void osnoise_tracer_reset(struct trace_array *tr)
  2557. {
  2558. osnoise_tracer_stop(tr);
  2559. }
  2560. static struct tracer osnoise_tracer __read_mostly = {
  2561. .name = "osnoise",
  2562. .init = osnoise_tracer_init,
  2563. .reset = osnoise_tracer_reset,
  2564. .start = osnoise_tracer_start,
  2565. .stop = osnoise_tracer_stop,
  2566. .print_header = print_osnoise_headers,
  2567. .allow_instances = true,
  2568. };
  2569. #ifdef CONFIG_TIMERLAT_TRACER
  2570. static void timerlat_tracer_start(struct trace_array *tr)
  2571. {
  2572. int retval;
  2573. /*
  2574. * If the instance is already registered, there is no need to
  2575. * register it again.
  2576. */
  2577. if (osnoise_instance_registered(tr))
  2578. return;
  2579. retval = osnoise_workload_start();
  2580. if (retval)
  2581. pr_err(BANNER "Error starting timerlat tracer\n");
  2582. osnoise_register_instance(tr);
  2583. return;
  2584. }
  2585. static void timerlat_tracer_stop(struct trace_array *tr)
  2586. {
  2587. int cpu;
  2588. osnoise_unregister_instance(tr);
  2589. /*
  2590. * Instruct the threads to stop only if this is the last instance.
  2591. */
  2592. if (!osnoise_has_registered_instances()) {
  2593. for_each_online_cpu(cpu)
  2594. per_cpu(per_cpu_osnoise_var, cpu).sampling = 0;
  2595. }
  2596. osnoise_workload_stop();
  2597. }
  2598. static int timerlat_tracer_init(struct trace_array *tr)
  2599. {
  2600. /*
  2601. * Only allow timerlat tracer if osnoise tracer is not running already.
  2602. */
  2603. if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer)
  2604. return -EBUSY;
  2605. /*
  2606. * If this is the first instance, set timerlat_tracer to block
  2607. * osnoise tracer start.
  2608. */
  2609. if (!osnoise_has_registered_instances())
  2610. osnoise_data.timerlat_tracer = 1;
  2611. tr->max_latency = 0;
  2612. timerlat_tracer_start(tr);
  2613. return 0;
  2614. }
  2615. static void timerlat_tracer_reset(struct trace_array *tr)
  2616. {
  2617. timerlat_tracer_stop(tr);
  2618. /*
  2619. * If this is the last instance, reset timerlat_tracer allowing
  2620. * osnoise to be started.
  2621. */
  2622. if (!osnoise_has_registered_instances())
  2623. osnoise_data.timerlat_tracer = 0;
  2624. }
  2625. static struct tracer timerlat_tracer __read_mostly = {
  2626. .name = "timerlat",
  2627. .init = timerlat_tracer_init,
  2628. .reset = timerlat_tracer_reset,
  2629. .start = timerlat_tracer_start,
  2630. .stop = timerlat_tracer_stop,
  2631. .print_header = print_timerlat_headers,
  2632. .allow_instances = true,
  2633. };
  2634. __init static int init_timerlat_tracer(void)
  2635. {
  2636. return register_tracer(&timerlat_tracer);
  2637. }
  2638. #else /* CONFIG_TIMERLAT_TRACER */
  2639. __init static int init_timerlat_tracer(void)
  2640. {
  2641. return 0;
  2642. }
  2643. #endif /* CONFIG_TIMERLAT_TRACER */
  2644. __init static int init_osnoise_tracer(void)
  2645. {
  2646. int ret;
  2647. mutex_init(&interface_lock);
  2648. cpumask_copy(&osnoise_cpumask, cpu_all_mask);
  2649. ret = register_tracer(&osnoise_tracer);
  2650. if (ret) {
  2651. pr_err(BANNER "Error registering osnoise!\n");
  2652. return ret;
  2653. }
  2654. ret = init_timerlat_tracer();
  2655. if (ret) {
  2656. pr_err(BANNER "Error registering timerlat!\n");
  2657. return ret;
  2658. }
  2659. osnoise_init_hotplug_support();
  2660. INIT_LIST_HEAD_RCU(&osnoise_instances);
  2661. init_tracefs();
  2662. return 0;
  2663. }
  2664. late_initcall(init_osnoise_tracer);