ring_buffer.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  1. /*
  2. * Generic ring buffer
  3. *
  4. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  5. */
  6. #include <linux/ring_buffer.h>
  7. #include <linux/trace_clock.h>
  8. #include <linux/ftrace_irq.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/module.h>
  14. #include <linux/percpu.h>
  15. #include <linux/mutex.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/list.h>
  19. #include <linux/cpu.h>
  20. #include <linux/fs.h>
  21. #include "trace.h"
  22. /*
  23. * The ring buffer header is special. We must manually up keep it.
  24. */
  25. int ring_buffer_print_entry_header(struct trace_seq *s)
  26. {
  27. int ret;
  28. ret = trace_seq_printf(s, "\ttype : 2 bits\n");
  29. ret = trace_seq_printf(s, "\tlen : 3 bits\n");
  30. ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
  31. ret = trace_seq_printf(s, "\tarray : 32 bits\n");
  32. ret = trace_seq_printf(s, "\n");
  33. ret = trace_seq_printf(s, "\tpadding : type == %d\n",
  34. RINGBUF_TYPE_PADDING);
  35. ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
  36. RINGBUF_TYPE_TIME_EXTEND);
  37. ret = trace_seq_printf(s, "\tdata : type == %d\n",
  38. RINGBUF_TYPE_DATA);
  39. return ret;
  40. }
  41. /*
  42. * The ring buffer is made up of a list of pages. A separate list of pages is
  43. * allocated for each CPU. A writer may only write to a buffer that is
  44. * associated with the CPU it is currently executing on. A reader may read
  45. * from any per cpu buffer.
  46. *
  47. * The reader is special. For each per cpu buffer, the reader has its own
  48. * reader page. When a reader has read the entire reader page, this reader
  49. * page is swapped with another page in the ring buffer.
  50. *
  51. * Now, as long as the writer is off the reader page, the reader can do what
  52. * ever it wants with that page. The writer will never write to that page
  53. * again (as long as it is out of the ring buffer).
  54. *
  55. * Here's some silly ASCII art.
  56. *
  57. * +------+
  58. * |reader| RING BUFFER
  59. * |page |
  60. * +------+ +---+ +---+ +---+
  61. * | |-->| |-->| |
  62. * +---+ +---+ +---+
  63. * ^ |
  64. * | |
  65. * +---------------+
  66. *
  67. *
  68. * +------+
  69. * |reader| RING BUFFER
  70. * |page |------------------v
  71. * +------+ +---+ +---+ +---+
  72. * | |-->| |-->| |
  73. * +---+ +---+ +---+
  74. * ^ |
  75. * | |
  76. * +---------------+
  77. *
  78. *
  79. * +------+
  80. * |reader| RING BUFFER
  81. * |page |------------------v
  82. * +------+ +---+ +---+ +---+
  83. * ^ | |-->| |-->| |
  84. * | +---+ +---+ +---+
  85. * | |
  86. * | |
  87. * +------------------------------+
  88. *
  89. *
  90. * +------+
  91. * |buffer| RING BUFFER
  92. * |page |------------------v
  93. * +------+ +---+ +---+ +---+
  94. * ^ | | | |-->| |
  95. * | New +---+ +---+ +---+
  96. * | Reader------^ |
  97. * | page |
  98. * +------------------------------+
  99. *
  100. *
  101. * After we make this swap, the reader can hand this page off to the splice
  102. * code and be done with it. It can even allocate a new page if it needs to
  103. * and swap that into the ring buffer.
  104. *
  105. * We will be using cmpxchg soon to make all this lockless.
  106. *
  107. */
  108. /*
  109. * A fast way to enable or disable all ring buffers is to
  110. * call tracing_on or tracing_off. Turning off the ring buffers
  111. * prevents all ring buffers from being recorded to.
  112. * Turning this switch on, makes it OK to write to the
  113. * ring buffer, if the ring buffer is enabled itself.
  114. *
  115. * There's three layers that must be on in order to write
  116. * to the ring buffer.
  117. *
  118. * 1) This global flag must be set.
  119. * 2) The ring buffer must be enabled for recording.
  120. * 3) The per cpu buffer must be enabled for recording.
  121. *
  122. * In case of an anomaly, this global flag has a bit set that
  123. * will permantly disable all ring buffers.
  124. */
  125. /*
  126. * Global flag to disable all recording to ring buffers
  127. * This has two bits: ON, DISABLED
  128. *
  129. * ON DISABLED
  130. * ---- ----------
  131. * 0 0 : ring buffers are off
  132. * 1 0 : ring buffers are on
  133. * X 1 : ring buffers are permanently disabled
  134. */
  135. enum {
  136. RB_BUFFERS_ON_BIT = 0,
  137. RB_BUFFERS_DISABLED_BIT = 1,
  138. };
  139. enum {
  140. RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
  141. RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
  142. };
  143. static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
  144. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  145. /**
  146. * tracing_on - enable all tracing buffers
  147. *
  148. * This function enables all tracing buffers that may have been
  149. * disabled with tracing_off.
  150. */
  151. void tracing_on(void)
  152. {
  153. set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  154. }
  155. EXPORT_SYMBOL_GPL(tracing_on);
  156. /**
  157. * tracing_off - turn off all tracing buffers
  158. *
  159. * This function stops all tracing buffers from recording data.
  160. * It does not disable any overhead the tracers themselves may
  161. * be causing. This function simply causes all recording to
  162. * the ring buffers to fail.
  163. */
  164. void tracing_off(void)
  165. {
  166. clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  167. }
  168. EXPORT_SYMBOL_GPL(tracing_off);
  169. /**
  170. * tracing_off_permanent - permanently disable ring buffers
  171. *
  172. * This function, once called, will disable all ring buffers
  173. * permanently.
  174. */
  175. void tracing_off_permanent(void)
  176. {
  177. set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
  178. }
  179. /**
  180. * tracing_is_on - show state of ring buffers enabled
  181. */
  182. int tracing_is_on(void)
  183. {
  184. return ring_buffer_flags == RB_BUFFERS_ON;
  185. }
  186. EXPORT_SYMBOL_GPL(tracing_is_on);
  187. #include "trace.h"
  188. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  189. #define RB_ALIGNMENT 4U
  190. #define RB_MAX_SMALL_DATA 28
  191. enum {
  192. RB_LEN_TIME_EXTEND = 8,
  193. RB_LEN_TIME_STAMP = 16,
  194. };
  195. static inline int rb_null_event(struct ring_buffer_event *event)
  196. {
  197. return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
  198. }
  199. static inline int rb_discarded_event(struct ring_buffer_event *event)
  200. {
  201. return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
  202. }
  203. static void rb_event_set_padding(struct ring_buffer_event *event)
  204. {
  205. event->type = RINGBUF_TYPE_PADDING;
  206. event->time_delta = 0;
  207. }
  208. static unsigned
  209. rb_event_data_length(struct ring_buffer_event *event)
  210. {
  211. unsigned length;
  212. if (event->len)
  213. length = event->len * RB_ALIGNMENT;
  214. else
  215. length = event->array[0];
  216. return length + RB_EVNT_HDR_SIZE;
  217. }
  218. /* inline for ring buffer fast paths */
  219. static unsigned
  220. rb_event_length(struct ring_buffer_event *event)
  221. {
  222. switch (event->type) {
  223. case RINGBUF_TYPE_PADDING:
  224. if (rb_null_event(event))
  225. /* undefined */
  226. return -1;
  227. return rb_event_data_length(event);
  228. case RINGBUF_TYPE_TIME_EXTEND:
  229. return RB_LEN_TIME_EXTEND;
  230. case RINGBUF_TYPE_TIME_STAMP:
  231. return RB_LEN_TIME_STAMP;
  232. case RINGBUF_TYPE_DATA:
  233. return rb_event_data_length(event);
  234. default:
  235. BUG();
  236. }
  237. /* not hit */
  238. return 0;
  239. }
  240. /**
  241. * ring_buffer_event_length - return the length of the event
  242. * @event: the event to get the length of
  243. */
  244. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  245. {
  246. unsigned length = rb_event_length(event);
  247. if (event->type != RINGBUF_TYPE_DATA)
  248. return length;
  249. length -= RB_EVNT_HDR_SIZE;
  250. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  251. length -= sizeof(event->array[0]);
  252. return length;
  253. }
  254. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  255. /* inline for ring buffer fast paths */
  256. static void *
  257. rb_event_data(struct ring_buffer_event *event)
  258. {
  259. BUG_ON(event->type != RINGBUF_TYPE_DATA);
  260. /* If length is in len field, then array[0] has the data */
  261. if (event->len)
  262. return (void *)&event->array[0];
  263. /* Otherwise length is in array[0] and array[1] has the data */
  264. return (void *)&event->array[1];
  265. }
  266. /**
  267. * ring_buffer_event_data - return the data of the event
  268. * @event: the event to get the data from
  269. */
  270. void *ring_buffer_event_data(struct ring_buffer_event *event)
  271. {
  272. return rb_event_data(event);
  273. }
  274. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  275. #define for_each_buffer_cpu(buffer, cpu) \
  276. for_each_cpu(cpu, buffer->cpumask)
  277. #define TS_SHIFT 27
  278. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  279. #define TS_DELTA_TEST (~TS_MASK)
  280. struct buffer_data_page {
  281. u64 time_stamp; /* page time stamp */
  282. local_t commit; /* write committed index */
  283. unsigned char data[]; /* data of buffer page */
  284. };
  285. struct buffer_page {
  286. local_t write; /* index for next write */
  287. unsigned read; /* index for next read */
  288. struct list_head list; /* list of free pages */
  289. struct buffer_data_page *page; /* Actual data page */
  290. };
  291. static void rb_init_page(struct buffer_data_page *bpage)
  292. {
  293. local_set(&bpage->commit, 0);
  294. }
  295. /**
  296. * ring_buffer_page_len - the size of data on the page.
  297. * @page: The page to read
  298. *
  299. * Returns the amount of data on the page, including buffer page header.
  300. */
  301. size_t ring_buffer_page_len(void *page)
  302. {
  303. return local_read(&((struct buffer_data_page *)page)->commit)
  304. + BUF_PAGE_HDR_SIZE;
  305. }
  306. /*
  307. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  308. * this issue out.
  309. */
  310. static void free_buffer_page(struct buffer_page *bpage)
  311. {
  312. free_page((unsigned long)bpage->page);
  313. kfree(bpage);
  314. }
  315. /*
  316. * We need to fit the time_stamp delta into 27 bits.
  317. */
  318. static inline int test_time_stamp(u64 delta)
  319. {
  320. if (delta & TS_DELTA_TEST)
  321. return 1;
  322. return 0;
  323. }
  324. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  325. int ring_buffer_print_page_header(struct trace_seq *s)
  326. {
  327. struct buffer_data_page field;
  328. int ret;
  329. ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  330. "offset:0;\tsize:%u;\n",
  331. (unsigned int)sizeof(field.time_stamp));
  332. ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
  333. "offset:%u;\tsize:%u;\n",
  334. (unsigned int)offsetof(typeof(field), commit),
  335. (unsigned int)sizeof(field.commit));
  336. ret = trace_seq_printf(s, "\tfield: char data;\t"
  337. "offset:%u;\tsize:%u;\n",
  338. (unsigned int)offsetof(typeof(field), data),
  339. (unsigned int)BUF_PAGE_SIZE);
  340. return ret;
  341. }
  342. /*
  343. * head_page == tail_page && head == tail then buffer is empty.
  344. */
  345. struct ring_buffer_per_cpu {
  346. int cpu;
  347. struct ring_buffer *buffer;
  348. spinlock_t reader_lock; /* serialize readers */
  349. raw_spinlock_t lock;
  350. struct lock_class_key lock_key;
  351. struct list_head pages;
  352. struct buffer_page *head_page; /* read from head */
  353. struct buffer_page *tail_page; /* write to tail */
  354. struct buffer_page *commit_page; /* committed pages */
  355. struct buffer_page *reader_page;
  356. unsigned long overrun;
  357. unsigned long entries;
  358. u64 write_stamp;
  359. u64 read_stamp;
  360. atomic_t record_disabled;
  361. };
  362. struct ring_buffer {
  363. unsigned pages;
  364. unsigned flags;
  365. int cpus;
  366. atomic_t record_disabled;
  367. cpumask_var_t cpumask;
  368. struct mutex mutex;
  369. struct ring_buffer_per_cpu **buffers;
  370. #ifdef CONFIG_HOTPLUG_CPU
  371. struct notifier_block cpu_notify;
  372. #endif
  373. u64 (*clock)(void);
  374. };
  375. struct ring_buffer_iter {
  376. struct ring_buffer_per_cpu *cpu_buffer;
  377. unsigned long head;
  378. struct buffer_page *head_page;
  379. u64 read_stamp;
  380. };
  381. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  382. #define RB_WARN_ON(buffer, cond) \
  383. ({ \
  384. int _____ret = unlikely(cond); \
  385. if (_____ret) { \
  386. atomic_inc(&buffer->record_disabled); \
  387. WARN_ON(1); \
  388. } \
  389. _____ret; \
  390. })
  391. /* Up this if you want to test the TIME_EXTENTS and normalization */
  392. #define DEBUG_SHIFT 0
  393. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  394. {
  395. u64 time;
  396. preempt_disable_notrace();
  397. /* shift to debug/test normalization and TIME_EXTENTS */
  398. time = buffer->clock() << DEBUG_SHIFT;
  399. preempt_enable_no_resched_notrace();
  400. return time;
  401. }
  402. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  403. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  404. int cpu, u64 *ts)
  405. {
  406. /* Just stupid testing the normalize function and deltas */
  407. *ts >>= DEBUG_SHIFT;
  408. }
  409. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  410. /**
  411. * check_pages - integrity check of buffer pages
  412. * @cpu_buffer: CPU buffer with pages to test
  413. *
  414. * As a safety measure we check to make sure the data pages have not
  415. * been corrupted.
  416. */
  417. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  418. {
  419. struct list_head *head = &cpu_buffer->pages;
  420. struct buffer_page *bpage, *tmp;
  421. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  422. return -1;
  423. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  424. return -1;
  425. list_for_each_entry_safe(bpage, tmp, head, list) {
  426. if (RB_WARN_ON(cpu_buffer,
  427. bpage->list.next->prev != &bpage->list))
  428. return -1;
  429. if (RB_WARN_ON(cpu_buffer,
  430. bpage->list.prev->next != &bpage->list))
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  436. unsigned nr_pages)
  437. {
  438. struct list_head *head = &cpu_buffer->pages;
  439. struct buffer_page *bpage, *tmp;
  440. unsigned long addr;
  441. LIST_HEAD(pages);
  442. unsigned i;
  443. for (i = 0; i < nr_pages; i++) {
  444. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  445. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  446. if (!bpage)
  447. goto free_pages;
  448. list_add(&bpage->list, &pages);
  449. addr = __get_free_page(GFP_KERNEL);
  450. if (!addr)
  451. goto free_pages;
  452. bpage->page = (void *)addr;
  453. rb_init_page(bpage->page);
  454. }
  455. list_splice(&pages, head);
  456. rb_check_pages(cpu_buffer);
  457. return 0;
  458. free_pages:
  459. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  460. list_del_init(&bpage->list);
  461. free_buffer_page(bpage);
  462. }
  463. return -ENOMEM;
  464. }
  465. static struct ring_buffer_per_cpu *
  466. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  467. {
  468. struct ring_buffer_per_cpu *cpu_buffer;
  469. struct buffer_page *bpage;
  470. unsigned long addr;
  471. int ret;
  472. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  473. GFP_KERNEL, cpu_to_node(cpu));
  474. if (!cpu_buffer)
  475. return NULL;
  476. cpu_buffer->cpu = cpu;
  477. cpu_buffer->buffer = buffer;
  478. spin_lock_init(&cpu_buffer->reader_lock);
  479. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  480. INIT_LIST_HEAD(&cpu_buffer->pages);
  481. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  482. GFP_KERNEL, cpu_to_node(cpu));
  483. if (!bpage)
  484. goto fail_free_buffer;
  485. cpu_buffer->reader_page = bpage;
  486. addr = __get_free_page(GFP_KERNEL);
  487. if (!addr)
  488. goto fail_free_reader;
  489. bpage->page = (void *)addr;
  490. rb_init_page(bpage->page);
  491. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  492. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  493. if (ret < 0)
  494. goto fail_free_reader;
  495. cpu_buffer->head_page
  496. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  497. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  498. return cpu_buffer;
  499. fail_free_reader:
  500. free_buffer_page(cpu_buffer->reader_page);
  501. fail_free_buffer:
  502. kfree(cpu_buffer);
  503. return NULL;
  504. }
  505. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  506. {
  507. struct list_head *head = &cpu_buffer->pages;
  508. struct buffer_page *bpage, *tmp;
  509. free_buffer_page(cpu_buffer->reader_page);
  510. list_for_each_entry_safe(bpage, tmp, head, list) {
  511. list_del_init(&bpage->list);
  512. free_buffer_page(bpage);
  513. }
  514. kfree(cpu_buffer);
  515. }
  516. /*
  517. * Causes compile errors if the struct buffer_page gets bigger
  518. * than the struct page.
  519. */
  520. extern int ring_buffer_page_too_big(void);
  521. #ifdef CONFIG_HOTPLUG_CPU
  522. static int rb_cpu_notify(struct notifier_block *self,
  523. unsigned long action, void *hcpu);
  524. #endif
  525. /**
  526. * ring_buffer_alloc - allocate a new ring_buffer
  527. * @size: the size in bytes per cpu that is needed.
  528. * @flags: attributes to set for the ring buffer.
  529. *
  530. * Currently the only flag that is available is the RB_FL_OVERWRITE
  531. * flag. This flag means that the buffer will overwrite old data
  532. * when the buffer wraps. If this flag is not set, the buffer will
  533. * drop data when the tail hits the head.
  534. */
  535. struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
  536. {
  537. struct ring_buffer *buffer;
  538. int bsize;
  539. int cpu;
  540. /* Paranoid! Optimizes out when all is well */
  541. if (sizeof(struct buffer_page) > sizeof(struct page))
  542. ring_buffer_page_too_big();
  543. /* keep it in its own cache line */
  544. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  545. GFP_KERNEL);
  546. if (!buffer)
  547. return NULL;
  548. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  549. goto fail_free_buffer;
  550. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  551. buffer->flags = flags;
  552. buffer->clock = trace_clock_local;
  553. /* need at least two pages */
  554. if (buffer->pages == 1)
  555. buffer->pages++;
  556. /*
  557. * In case of non-hotplug cpu, if the ring-buffer is allocated
  558. * in early initcall, it will not be notified of secondary cpus.
  559. * In that off case, we need to allocate for all possible cpus.
  560. */
  561. #ifdef CONFIG_HOTPLUG_CPU
  562. get_online_cpus();
  563. cpumask_copy(buffer->cpumask, cpu_online_mask);
  564. #else
  565. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  566. #endif
  567. buffer->cpus = nr_cpu_ids;
  568. bsize = sizeof(void *) * nr_cpu_ids;
  569. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  570. GFP_KERNEL);
  571. if (!buffer->buffers)
  572. goto fail_free_cpumask;
  573. for_each_buffer_cpu(buffer, cpu) {
  574. buffer->buffers[cpu] =
  575. rb_allocate_cpu_buffer(buffer, cpu);
  576. if (!buffer->buffers[cpu])
  577. goto fail_free_buffers;
  578. }
  579. #ifdef CONFIG_HOTPLUG_CPU
  580. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  581. buffer->cpu_notify.priority = 0;
  582. register_cpu_notifier(&buffer->cpu_notify);
  583. #endif
  584. put_online_cpus();
  585. mutex_init(&buffer->mutex);
  586. return buffer;
  587. fail_free_buffers:
  588. for_each_buffer_cpu(buffer, cpu) {
  589. if (buffer->buffers[cpu])
  590. rb_free_cpu_buffer(buffer->buffers[cpu]);
  591. }
  592. kfree(buffer->buffers);
  593. fail_free_cpumask:
  594. free_cpumask_var(buffer->cpumask);
  595. put_online_cpus();
  596. fail_free_buffer:
  597. kfree(buffer);
  598. return NULL;
  599. }
  600. EXPORT_SYMBOL_GPL(ring_buffer_alloc);
  601. /**
  602. * ring_buffer_free - free a ring buffer.
  603. * @buffer: the buffer to free.
  604. */
  605. void
  606. ring_buffer_free(struct ring_buffer *buffer)
  607. {
  608. int cpu;
  609. get_online_cpus();
  610. #ifdef CONFIG_HOTPLUG_CPU
  611. unregister_cpu_notifier(&buffer->cpu_notify);
  612. #endif
  613. for_each_buffer_cpu(buffer, cpu)
  614. rb_free_cpu_buffer(buffer->buffers[cpu]);
  615. put_online_cpus();
  616. free_cpumask_var(buffer->cpumask);
  617. kfree(buffer);
  618. }
  619. EXPORT_SYMBOL_GPL(ring_buffer_free);
  620. void ring_buffer_set_clock(struct ring_buffer *buffer,
  621. u64 (*clock)(void))
  622. {
  623. buffer->clock = clock;
  624. }
  625. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  626. static void
  627. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  628. {
  629. struct buffer_page *bpage;
  630. struct list_head *p;
  631. unsigned i;
  632. atomic_inc(&cpu_buffer->record_disabled);
  633. synchronize_sched();
  634. for (i = 0; i < nr_pages; i++) {
  635. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  636. return;
  637. p = cpu_buffer->pages.next;
  638. bpage = list_entry(p, struct buffer_page, list);
  639. list_del_init(&bpage->list);
  640. free_buffer_page(bpage);
  641. }
  642. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  643. return;
  644. rb_reset_cpu(cpu_buffer);
  645. rb_check_pages(cpu_buffer);
  646. atomic_dec(&cpu_buffer->record_disabled);
  647. }
  648. static void
  649. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  650. struct list_head *pages, unsigned nr_pages)
  651. {
  652. struct buffer_page *bpage;
  653. struct list_head *p;
  654. unsigned i;
  655. atomic_inc(&cpu_buffer->record_disabled);
  656. synchronize_sched();
  657. for (i = 0; i < nr_pages; i++) {
  658. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  659. return;
  660. p = pages->next;
  661. bpage = list_entry(p, struct buffer_page, list);
  662. list_del_init(&bpage->list);
  663. list_add_tail(&bpage->list, &cpu_buffer->pages);
  664. }
  665. rb_reset_cpu(cpu_buffer);
  666. rb_check_pages(cpu_buffer);
  667. atomic_dec(&cpu_buffer->record_disabled);
  668. }
  669. /**
  670. * ring_buffer_resize - resize the ring buffer
  671. * @buffer: the buffer to resize.
  672. * @size: the new size.
  673. *
  674. * The tracer is responsible for making sure that the buffer is
  675. * not being used while changing the size.
  676. * Note: We may be able to change the above requirement by using
  677. * RCU synchronizations.
  678. *
  679. * Minimum size is 2 * BUF_PAGE_SIZE.
  680. *
  681. * Returns -1 on failure.
  682. */
  683. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  684. {
  685. struct ring_buffer_per_cpu *cpu_buffer;
  686. unsigned nr_pages, rm_pages, new_pages;
  687. struct buffer_page *bpage, *tmp;
  688. unsigned long buffer_size;
  689. unsigned long addr;
  690. LIST_HEAD(pages);
  691. int i, cpu;
  692. /*
  693. * Always succeed at resizing a non-existent buffer:
  694. */
  695. if (!buffer)
  696. return size;
  697. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  698. size *= BUF_PAGE_SIZE;
  699. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  700. /* we need a minimum of two pages */
  701. if (size < BUF_PAGE_SIZE * 2)
  702. size = BUF_PAGE_SIZE * 2;
  703. if (size == buffer_size)
  704. return size;
  705. mutex_lock(&buffer->mutex);
  706. get_online_cpus();
  707. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  708. if (size < buffer_size) {
  709. /* easy case, just free pages */
  710. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  711. goto out_fail;
  712. rm_pages = buffer->pages - nr_pages;
  713. for_each_buffer_cpu(buffer, cpu) {
  714. cpu_buffer = buffer->buffers[cpu];
  715. rb_remove_pages(cpu_buffer, rm_pages);
  716. }
  717. goto out;
  718. }
  719. /*
  720. * This is a bit more difficult. We only want to add pages
  721. * when we can allocate enough for all CPUs. We do this
  722. * by allocating all the pages and storing them on a local
  723. * link list. If we succeed in our allocation, then we
  724. * add these pages to the cpu_buffers. Otherwise we just free
  725. * them all and return -ENOMEM;
  726. */
  727. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  728. goto out_fail;
  729. new_pages = nr_pages - buffer->pages;
  730. for_each_buffer_cpu(buffer, cpu) {
  731. for (i = 0; i < new_pages; i++) {
  732. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  733. cache_line_size()),
  734. GFP_KERNEL, cpu_to_node(cpu));
  735. if (!bpage)
  736. goto free_pages;
  737. list_add(&bpage->list, &pages);
  738. addr = __get_free_page(GFP_KERNEL);
  739. if (!addr)
  740. goto free_pages;
  741. bpage->page = (void *)addr;
  742. rb_init_page(bpage->page);
  743. }
  744. }
  745. for_each_buffer_cpu(buffer, cpu) {
  746. cpu_buffer = buffer->buffers[cpu];
  747. rb_insert_pages(cpu_buffer, &pages, new_pages);
  748. }
  749. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  750. goto out_fail;
  751. out:
  752. buffer->pages = nr_pages;
  753. put_online_cpus();
  754. mutex_unlock(&buffer->mutex);
  755. return size;
  756. free_pages:
  757. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  758. list_del_init(&bpage->list);
  759. free_buffer_page(bpage);
  760. }
  761. put_online_cpus();
  762. mutex_unlock(&buffer->mutex);
  763. return -ENOMEM;
  764. /*
  765. * Something went totally wrong, and we are too paranoid
  766. * to even clean up the mess.
  767. */
  768. out_fail:
  769. put_online_cpus();
  770. mutex_unlock(&buffer->mutex);
  771. return -1;
  772. }
  773. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  774. static inline void *
  775. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  776. {
  777. return bpage->data + index;
  778. }
  779. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  780. {
  781. return bpage->page->data + index;
  782. }
  783. static inline struct ring_buffer_event *
  784. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  785. {
  786. return __rb_page_index(cpu_buffer->reader_page,
  787. cpu_buffer->reader_page->read);
  788. }
  789. static inline struct ring_buffer_event *
  790. rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
  791. {
  792. return __rb_page_index(cpu_buffer->head_page,
  793. cpu_buffer->head_page->read);
  794. }
  795. static inline struct ring_buffer_event *
  796. rb_iter_head_event(struct ring_buffer_iter *iter)
  797. {
  798. return __rb_page_index(iter->head_page, iter->head);
  799. }
  800. static inline unsigned rb_page_write(struct buffer_page *bpage)
  801. {
  802. return local_read(&bpage->write);
  803. }
  804. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  805. {
  806. return local_read(&bpage->page->commit);
  807. }
  808. /* Size is determined by what has been commited */
  809. static inline unsigned rb_page_size(struct buffer_page *bpage)
  810. {
  811. return rb_page_commit(bpage);
  812. }
  813. static inline unsigned
  814. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  815. {
  816. return rb_page_commit(cpu_buffer->commit_page);
  817. }
  818. static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
  819. {
  820. return rb_page_commit(cpu_buffer->head_page);
  821. }
  822. /*
  823. * When the tail hits the head and the buffer is in overwrite mode,
  824. * the head jumps to the next page and all content on the previous
  825. * page is discarded. But before doing so, we update the overrun
  826. * variable of the buffer.
  827. */
  828. static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
  829. {
  830. struct ring_buffer_event *event;
  831. unsigned long head;
  832. for (head = 0; head < rb_head_size(cpu_buffer);
  833. head += rb_event_length(event)) {
  834. event = __rb_page_index(cpu_buffer->head_page, head);
  835. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  836. return;
  837. /* Only count data entries */
  838. if (event->type != RINGBUF_TYPE_DATA)
  839. continue;
  840. cpu_buffer->overrun++;
  841. cpu_buffer->entries--;
  842. }
  843. }
  844. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  845. struct buffer_page **bpage)
  846. {
  847. struct list_head *p = (*bpage)->list.next;
  848. if (p == &cpu_buffer->pages)
  849. p = p->next;
  850. *bpage = list_entry(p, struct buffer_page, list);
  851. }
  852. static inline unsigned
  853. rb_event_index(struct ring_buffer_event *event)
  854. {
  855. unsigned long addr = (unsigned long)event;
  856. return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
  857. }
  858. static int
  859. rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  860. struct ring_buffer_event *event)
  861. {
  862. unsigned long addr = (unsigned long)event;
  863. unsigned long index;
  864. index = rb_event_index(event);
  865. addr &= PAGE_MASK;
  866. return cpu_buffer->commit_page->page == (void *)addr &&
  867. rb_commit_index(cpu_buffer) == index;
  868. }
  869. static void
  870. rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
  871. struct ring_buffer_event *event)
  872. {
  873. unsigned long addr = (unsigned long)event;
  874. unsigned long index;
  875. index = rb_event_index(event);
  876. addr &= PAGE_MASK;
  877. while (cpu_buffer->commit_page->page != (void *)addr) {
  878. if (RB_WARN_ON(cpu_buffer,
  879. cpu_buffer->commit_page == cpu_buffer->tail_page))
  880. return;
  881. cpu_buffer->commit_page->page->commit =
  882. cpu_buffer->commit_page->write;
  883. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  884. cpu_buffer->write_stamp =
  885. cpu_buffer->commit_page->page->time_stamp;
  886. }
  887. /* Now set the commit to the event's index */
  888. local_set(&cpu_buffer->commit_page->page->commit, index);
  889. }
  890. static void
  891. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  892. {
  893. /*
  894. * We only race with interrupts and NMIs on this CPU.
  895. * If we own the commit event, then we can commit
  896. * all others that interrupted us, since the interruptions
  897. * are in stack format (they finish before they come
  898. * back to us). This allows us to do a simple loop to
  899. * assign the commit to the tail.
  900. */
  901. again:
  902. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  903. cpu_buffer->commit_page->page->commit =
  904. cpu_buffer->commit_page->write;
  905. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  906. cpu_buffer->write_stamp =
  907. cpu_buffer->commit_page->page->time_stamp;
  908. /* add barrier to keep gcc from optimizing too much */
  909. barrier();
  910. }
  911. while (rb_commit_index(cpu_buffer) !=
  912. rb_page_write(cpu_buffer->commit_page)) {
  913. cpu_buffer->commit_page->page->commit =
  914. cpu_buffer->commit_page->write;
  915. barrier();
  916. }
  917. /* again, keep gcc from optimizing */
  918. barrier();
  919. /*
  920. * If an interrupt came in just after the first while loop
  921. * and pushed the tail page forward, we will be left with
  922. * a dangling commit that will never go forward.
  923. */
  924. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  925. goto again;
  926. }
  927. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  928. {
  929. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  930. cpu_buffer->reader_page->read = 0;
  931. }
  932. static void rb_inc_iter(struct ring_buffer_iter *iter)
  933. {
  934. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  935. /*
  936. * The iterator could be on the reader page (it starts there).
  937. * But the head could have moved, since the reader was
  938. * found. Check for this case and assign the iterator
  939. * to the head page instead of next.
  940. */
  941. if (iter->head_page == cpu_buffer->reader_page)
  942. iter->head_page = cpu_buffer->head_page;
  943. else
  944. rb_inc_page(cpu_buffer, &iter->head_page);
  945. iter->read_stamp = iter->head_page->page->time_stamp;
  946. iter->head = 0;
  947. }
  948. /**
  949. * ring_buffer_update_event - update event type and data
  950. * @event: the even to update
  951. * @type: the type of event
  952. * @length: the size of the event field in the ring buffer
  953. *
  954. * Update the type and data fields of the event. The length
  955. * is the actual size that is written to the ring buffer,
  956. * and with this, we can determine what to place into the
  957. * data field.
  958. */
  959. static void
  960. rb_update_event(struct ring_buffer_event *event,
  961. unsigned type, unsigned length)
  962. {
  963. event->type = type;
  964. switch (type) {
  965. case RINGBUF_TYPE_PADDING:
  966. break;
  967. case RINGBUF_TYPE_TIME_EXTEND:
  968. event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
  969. break;
  970. case RINGBUF_TYPE_TIME_STAMP:
  971. event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
  972. break;
  973. case RINGBUF_TYPE_DATA:
  974. length -= RB_EVNT_HDR_SIZE;
  975. if (length > RB_MAX_SMALL_DATA) {
  976. event->len = 0;
  977. event->array[0] = length;
  978. } else
  979. event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  980. break;
  981. default:
  982. BUG();
  983. }
  984. }
  985. static unsigned rb_calculate_event_length(unsigned length)
  986. {
  987. struct ring_buffer_event event; /* Used only for sizeof array */
  988. /* zero length can cause confusions */
  989. if (!length)
  990. length = 1;
  991. if (length > RB_MAX_SMALL_DATA)
  992. length += sizeof(event.array[0]);
  993. length += RB_EVNT_HDR_SIZE;
  994. length = ALIGN(length, RB_ALIGNMENT);
  995. return length;
  996. }
  997. static struct ring_buffer_event *
  998. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  999. unsigned type, unsigned long length, u64 *ts)
  1000. {
  1001. struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
  1002. unsigned long tail, write;
  1003. struct ring_buffer *buffer = cpu_buffer->buffer;
  1004. struct ring_buffer_event *event;
  1005. unsigned long flags;
  1006. bool lock_taken = false;
  1007. commit_page = cpu_buffer->commit_page;
  1008. /* we just need to protect against interrupts */
  1009. barrier();
  1010. tail_page = cpu_buffer->tail_page;
  1011. write = local_add_return(length, &tail_page->write);
  1012. tail = write - length;
  1013. /* See if we shot pass the end of this buffer page */
  1014. if (write > BUF_PAGE_SIZE) {
  1015. struct buffer_page *next_page = tail_page;
  1016. local_irq_save(flags);
  1017. /*
  1018. * Since the write to the buffer is still not
  1019. * fully lockless, we must be careful with NMIs.
  1020. * The locks in the writers are taken when a write
  1021. * crosses to a new page. The locks protect against
  1022. * races with the readers (this will soon be fixed
  1023. * with a lockless solution).
  1024. *
  1025. * Because we can not protect against NMIs, and we
  1026. * want to keep traces reentrant, we need to manage
  1027. * what happens when we are in an NMI.
  1028. *
  1029. * NMIs can happen after we take the lock.
  1030. * If we are in an NMI, only take the lock
  1031. * if it is not already taken. Otherwise
  1032. * simply fail.
  1033. */
  1034. if (unlikely(in_nmi())) {
  1035. if (!__raw_spin_trylock(&cpu_buffer->lock))
  1036. goto out_reset;
  1037. } else
  1038. __raw_spin_lock(&cpu_buffer->lock);
  1039. lock_taken = true;
  1040. rb_inc_page(cpu_buffer, &next_page);
  1041. head_page = cpu_buffer->head_page;
  1042. reader_page = cpu_buffer->reader_page;
  1043. /* we grabbed the lock before incrementing */
  1044. if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
  1045. goto out_reset;
  1046. /*
  1047. * If for some reason, we had an interrupt storm that made
  1048. * it all the way around the buffer, bail, and warn
  1049. * about it.
  1050. */
  1051. if (unlikely(next_page == commit_page)) {
  1052. /* This can easily happen on small ring buffers */
  1053. WARN_ON_ONCE(buffer->pages > 2);
  1054. goto out_reset;
  1055. }
  1056. if (next_page == head_page) {
  1057. if (!(buffer->flags & RB_FL_OVERWRITE))
  1058. goto out_reset;
  1059. /* tail_page has not moved yet? */
  1060. if (tail_page == cpu_buffer->tail_page) {
  1061. /* count overflows */
  1062. rb_update_overflow(cpu_buffer);
  1063. rb_inc_page(cpu_buffer, &head_page);
  1064. cpu_buffer->head_page = head_page;
  1065. cpu_buffer->head_page->read = 0;
  1066. }
  1067. }
  1068. /*
  1069. * If the tail page is still the same as what we think
  1070. * it is, then it is up to us to update the tail
  1071. * pointer.
  1072. */
  1073. if (tail_page == cpu_buffer->tail_page) {
  1074. local_set(&next_page->write, 0);
  1075. local_set(&next_page->page->commit, 0);
  1076. cpu_buffer->tail_page = next_page;
  1077. /* reread the time stamp */
  1078. *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
  1079. cpu_buffer->tail_page->page->time_stamp = *ts;
  1080. }
  1081. /*
  1082. * The actual tail page has moved forward.
  1083. */
  1084. if (tail < BUF_PAGE_SIZE) {
  1085. /* Mark the rest of the page with padding */
  1086. event = __rb_page_index(tail_page, tail);
  1087. rb_event_set_padding(event);
  1088. }
  1089. if (tail <= BUF_PAGE_SIZE)
  1090. /* Set the write back to the previous setting */
  1091. local_set(&tail_page->write, tail);
  1092. /*
  1093. * If this was a commit entry that failed,
  1094. * increment that too
  1095. */
  1096. if (tail_page == cpu_buffer->commit_page &&
  1097. tail == rb_commit_index(cpu_buffer)) {
  1098. rb_set_commit_to_write(cpu_buffer);
  1099. }
  1100. __raw_spin_unlock(&cpu_buffer->lock);
  1101. local_irq_restore(flags);
  1102. /* fail and let the caller try again */
  1103. return ERR_PTR(-EAGAIN);
  1104. }
  1105. /* We reserved something on the buffer */
  1106. if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
  1107. return NULL;
  1108. event = __rb_page_index(tail_page, tail);
  1109. rb_update_event(event, type, length);
  1110. /*
  1111. * If this is a commit and the tail is zero, then update
  1112. * this page's time stamp.
  1113. */
  1114. if (!tail && rb_is_commit(cpu_buffer, event))
  1115. cpu_buffer->commit_page->page->time_stamp = *ts;
  1116. return event;
  1117. out_reset:
  1118. /* reset write */
  1119. if (tail <= BUF_PAGE_SIZE)
  1120. local_set(&tail_page->write, tail);
  1121. if (likely(lock_taken))
  1122. __raw_spin_unlock(&cpu_buffer->lock);
  1123. local_irq_restore(flags);
  1124. return NULL;
  1125. }
  1126. static int
  1127. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1128. u64 *ts, u64 *delta)
  1129. {
  1130. struct ring_buffer_event *event;
  1131. static int once;
  1132. int ret;
  1133. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1134. printk(KERN_WARNING "Delta way too big! %llu"
  1135. " ts=%llu write stamp = %llu\n",
  1136. (unsigned long long)*delta,
  1137. (unsigned long long)*ts,
  1138. (unsigned long long)cpu_buffer->write_stamp);
  1139. WARN_ON(1);
  1140. }
  1141. /*
  1142. * The delta is too big, we to add a
  1143. * new timestamp.
  1144. */
  1145. event = __rb_reserve_next(cpu_buffer,
  1146. RINGBUF_TYPE_TIME_EXTEND,
  1147. RB_LEN_TIME_EXTEND,
  1148. ts);
  1149. if (!event)
  1150. return -EBUSY;
  1151. if (PTR_ERR(event) == -EAGAIN)
  1152. return -EAGAIN;
  1153. /* Only a commited time event can update the write stamp */
  1154. if (rb_is_commit(cpu_buffer, event)) {
  1155. /*
  1156. * If this is the first on the page, then we need to
  1157. * update the page itself, and just put in a zero.
  1158. */
  1159. if (rb_event_index(event)) {
  1160. event->time_delta = *delta & TS_MASK;
  1161. event->array[0] = *delta >> TS_SHIFT;
  1162. } else {
  1163. cpu_buffer->commit_page->page->time_stamp = *ts;
  1164. event->time_delta = 0;
  1165. event->array[0] = 0;
  1166. }
  1167. cpu_buffer->write_stamp = *ts;
  1168. /* let the caller know this was the commit */
  1169. ret = 1;
  1170. } else {
  1171. /* Darn, this is just wasted space */
  1172. event->time_delta = 0;
  1173. event->array[0] = 0;
  1174. ret = 0;
  1175. }
  1176. *delta = 0;
  1177. return ret;
  1178. }
  1179. static struct ring_buffer_event *
  1180. rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
  1181. unsigned type, unsigned long length)
  1182. {
  1183. struct ring_buffer_event *event;
  1184. u64 ts, delta;
  1185. int commit = 0;
  1186. int nr_loops = 0;
  1187. again:
  1188. /*
  1189. * We allow for interrupts to reenter here and do a trace.
  1190. * If one does, it will cause this original code to loop
  1191. * back here. Even with heavy interrupts happening, this
  1192. * should only happen a few times in a row. If this happens
  1193. * 1000 times in a row, there must be either an interrupt
  1194. * storm or we have something buggy.
  1195. * Bail!
  1196. */
  1197. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1198. return NULL;
  1199. ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
  1200. /*
  1201. * Only the first commit can update the timestamp.
  1202. * Yes there is a race here. If an interrupt comes in
  1203. * just after the conditional and it traces too, then it
  1204. * will also check the deltas. More than one timestamp may
  1205. * also be made. But only the entry that did the actual
  1206. * commit will be something other than zero.
  1207. */
  1208. if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1209. rb_page_write(cpu_buffer->tail_page) ==
  1210. rb_commit_index(cpu_buffer)) {
  1211. delta = ts - cpu_buffer->write_stamp;
  1212. /* make sure this delta is calculated here */
  1213. barrier();
  1214. /* Did the write stamp get updated already? */
  1215. if (unlikely(ts < cpu_buffer->write_stamp))
  1216. delta = 0;
  1217. if (test_time_stamp(delta)) {
  1218. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1219. if (commit == -EBUSY)
  1220. return NULL;
  1221. if (commit == -EAGAIN)
  1222. goto again;
  1223. RB_WARN_ON(cpu_buffer, commit < 0);
  1224. }
  1225. } else
  1226. /* Non commits have zero deltas */
  1227. delta = 0;
  1228. event = __rb_reserve_next(cpu_buffer, type, length, &ts);
  1229. if (PTR_ERR(event) == -EAGAIN)
  1230. goto again;
  1231. if (!event) {
  1232. if (unlikely(commit))
  1233. /*
  1234. * Ouch! We needed a timestamp and it was commited. But
  1235. * we didn't get our event reserved.
  1236. */
  1237. rb_set_commit_to_write(cpu_buffer);
  1238. return NULL;
  1239. }
  1240. /*
  1241. * If the timestamp was commited, make the commit our entry
  1242. * now so that we will update it when needed.
  1243. */
  1244. if (commit)
  1245. rb_set_commit_event(cpu_buffer, event);
  1246. else if (!rb_is_commit(cpu_buffer, event))
  1247. delta = 0;
  1248. event->time_delta = delta;
  1249. return event;
  1250. }
  1251. #define TRACE_RECURSIVE_DEPTH 16
  1252. static int trace_recursive_lock(void)
  1253. {
  1254. current->trace_recursion++;
  1255. if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
  1256. return 0;
  1257. /* Disable all tracing before we do anything else */
  1258. tracing_off_permanent();
  1259. printk_once(KERN_WARNING "Tracing recursion: depth[%d]:"
  1260. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1261. current->trace_recursion,
  1262. hardirq_count() >> HARDIRQ_SHIFT,
  1263. softirq_count() >> SOFTIRQ_SHIFT,
  1264. in_nmi());
  1265. WARN_ON_ONCE(1);
  1266. return -1;
  1267. }
  1268. static void trace_recursive_unlock(void)
  1269. {
  1270. WARN_ON_ONCE(!current->trace_recursion);
  1271. current->trace_recursion--;
  1272. }
  1273. static DEFINE_PER_CPU(int, rb_need_resched);
  1274. /**
  1275. * ring_buffer_lock_reserve - reserve a part of the buffer
  1276. * @buffer: the ring buffer to reserve from
  1277. * @length: the length of the data to reserve (excluding event header)
  1278. *
  1279. * Returns a reseverd event on the ring buffer to copy directly to.
  1280. * The user of this interface will need to get the body to write into
  1281. * and can use the ring_buffer_event_data() interface.
  1282. *
  1283. * The length is the length of the data needed, not the event length
  1284. * which also includes the event header.
  1285. *
  1286. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1287. * If NULL is returned, then nothing has been allocated or locked.
  1288. */
  1289. struct ring_buffer_event *
  1290. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1291. {
  1292. struct ring_buffer_per_cpu *cpu_buffer;
  1293. struct ring_buffer_event *event;
  1294. int cpu, resched;
  1295. if (ring_buffer_flags != RB_BUFFERS_ON)
  1296. return NULL;
  1297. if (atomic_read(&buffer->record_disabled))
  1298. return NULL;
  1299. /* If we are tracing schedule, we don't want to recurse */
  1300. resched = ftrace_preempt_disable();
  1301. if (trace_recursive_lock())
  1302. goto out_nocheck;
  1303. cpu = raw_smp_processor_id();
  1304. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1305. goto out;
  1306. cpu_buffer = buffer->buffers[cpu];
  1307. if (atomic_read(&cpu_buffer->record_disabled))
  1308. goto out;
  1309. length = rb_calculate_event_length(length);
  1310. if (length > BUF_PAGE_SIZE)
  1311. goto out;
  1312. event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
  1313. if (!event)
  1314. goto out;
  1315. /*
  1316. * Need to store resched state on this cpu.
  1317. * Only the first needs to.
  1318. */
  1319. if (preempt_count() == 1)
  1320. per_cpu(rb_need_resched, cpu) = resched;
  1321. return event;
  1322. out:
  1323. trace_recursive_unlock();
  1324. out_nocheck:
  1325. ftrace_preempt_enable(resched);
  1326. return NULL;
  1327. }
  1328. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1329. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1330. struct ring_buffer_event *event)
  1331. {
  1332. cpu_buffer->entries++;
  1333. /* Only process further if we own the commit */
  1334. if (!rb_is_commit(cpu_buffer, event))
  1335. return;
  1336. cpu_buffer->write_stamp += event->time_delta;
  1337. rb_set_commit_to_write(cpu_buffer);
  1338. }
  1339. /**
  1340. * ring_buffer_unlock_commit - commit a reserved
  1341. * @buffer: The buffer to commit to
  1342. * @event: The event pointer to commit.
  1343. *
  1344. * This commits the data to the ring buffer, and releases any locks held.
  1345. *
  1346. * Must be paired with ring_buffer_lock_reserve.
  1347. */
  1348. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1349. struct ring_buffer_event *event)
  1350. {
  1351. struct ring_buffer_per_cpu *cpu_buffer;
  1352. int cpu = raw_smp_processor_id();
  1353. cpu_buffer = buffer->buffers[cpu];
  1354. rb_commit(cpu_buffer, event);
  1355. trace_recursive_unlock();
  1356. /*
  1357. * Only the last preempt count needs to restore preemption.
  1358. */
  1359. if (preempt_count() == 1)
  1360. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1361. else
  1362. preempt_enable_no_resched_notrace();
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1366. static inline void rb_event_discard(struct ring_buffer_event *event)
  1367. {
  1368. event->type = RINGBUF_TYPE_PADDING;
  1369. /* time delta must be non zero */
  1370. if (!event->time_delta)
  1371. event->time_delta = 1;
  1372. }
  1373. /**
  1374. * ring_buffer_event_discard - discard any event in the ring buffer
  1375. * @event: the event to discard
  1376. *
  1377. * Sometimes a event that is in the ring buffer needs to be ignored.
  1378. * This function lets the user discard an event in the ring buffer
  1379. * and then that event will not be read later.
  1380. *
  1381. * Note, it is up to the user to be careful with this, and protect
  1382. * against races. If the user discards an event that has been consumed
  1383. * it is possible that it could corrupt the ring buffer.
  1384. */
  1385. void ring_buffer_event_discard(struct ring_buffer_event *event)
  1386. {
  1387. rb_event_discard(event);
  1388. }
  1389. EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
  1390. /**
  1391. * ring_buffer_commit_discard - discard an event that has not been committed
  1392. * @buffer: the ring buffer
  1393. * @event: non committed event to discard
  1394. *
  1395. * This is similar to ring_buffer_event_discard but must only be
  1396. * performed on an event that has not been committed yet. The difference
  1397. * is that this will also try to free the event from the ring buffer
  1398. * if another event has not been added behind it.
  1399. *
  1400. * If another event has been added behind it, it will set the event
  1401. * up as discarded, and perform the commit.
  1402. *
  1403. * If this function is called, do not call ring_buffer_unlock_commit on
  1404. * the event.
  1405. */
  1406. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  1407. struct ring_buffer_event *event)
  1408. {
  1409. struct ring_buffer_per_cpu *cpu_buffer;
  1410. unsigned long new_index, old_index;
  1411. struct buffer_page *bpage;
  1412. unsigned long index;
  1413. unsigned long addr;
  1414. int cpu;
  1415. /* The event is discarded regardless */
  1416. rb_event_discard(event);
  1417. /*
  1418. * This must only be called if the event has not been
  1419. * committed yet. Thus we can assume that preemption
  1420. * is still disabled.
  1421. */
  1422. RB_WARN_ON(buffer, !preempt_count());
  1423. cpu = smp_processor_id();
  1424. cpu_buffer = buffer->buffers[cpu];
  1425. new_index = rb_event_index(event);
  1426. old_index = new_index + rb_event_length(event);
  1427. addr = (unsigned long)event;
  1428. addr &= PAGE_MASK;
  1429. bpage = cpu_buffer->tail_page;
  1430. if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
  1431. /*
  1432. * This is on the tail page. It is possible that
  1433. * a write could come in and move the tail page
  1434. * and write to the next page. That is fine
  1435. * because we just shorten what is on this page.
  1436. */
  1437. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1438. if (index == old_index)
  1439. goto out;
  1440. }
  1441. /*
  1442. * The commit is still visible by the reader, so we
  1443. * must increment entries.
  1444. */
  1445. cpu_buffer->entries++;
  1446. out:
  1447. /*
  1448. * If a write came in and pushed the tail page
  1449. * we still need to update the commit pointer
  1450. * if we were the commit.
  1451. */
  1452. if (rb_is_commit(cpu_buffer, event))
  1453. rb_set_commit_to_write(cpu_buffer);
  1454. trace_recursive_unlock();
  1455. /*
  1456. * Only the last preempt count needs to restore preemption.
  1457. */
  1458. if (preempt_count() == 1)
  1459. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1460. else
  1461. preempt_enable_no_resched_notrace();
  1462. }
  1463. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  1464. /**
  1465. * ring_buffer_write - write data to the buffer without reserving
  1466. * @buffer: The ring buffer to write to.
  1467. * @length: The length of the data being written (excluding the event header)
  1468. * @data: The data to write to the buffer.
  1469. *
  1470. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  1471. * one function. If you already have the data to write to the buffer, it
  1472. * may be easier to simply call this function.
  1473. *
  1474. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  1475. * and not the length of the event which would hold the header.
  1476. */
  1477. int ring_buffer_write(struct ring_buffer *buffer,
  1478. unsigned long length,
  1479. void *data)
  1480. {
  1481. struct ring_buffer_per_cpu *cpu_buffer;
  1482. struct ring_buffer_event *event;
  1483. unsigned long event_length;
  1484. void *body;
  1485. int ret = -EBUSY;
  1486. int cpu, resched;
  1487. if (ring_buffer_flags != RB_BUFFERS_ON)
  1488. return -EBUSY;
  1489. if (atomic_read(&buffer->record_disabled))
  1490. return -EBUSY;
  1491. resched = ftrace_preempt_disable();
  1492. cpu = raw_smp_processor_id();
  1493. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1494. goto out;
  1495. cpu_buffer = buffer->buffers[cpu];
  1496. if (atomic_read(&cpu_buffer->record_disabled))
  1497. goto out;
  1498. event_length = rb_calculate_event_length(length);
  1499. event = rb_reserve_next_event(cpu_buffer,
  1500. RINGBUF_TYPE_DATA, event_length);
  1501. if (!event)
  1502. goto out;
  1503. body = rb_event_data(event);
  1504. memcpy(body, data, length);
  1505. rb_commit(cpu_buffer, event);
  1506. ret = 0;
  1507. out:
  1508. ftrace_preempt_enable(resched);
  1509. return ret;
  1510. }
  1511. EXPORT_SYMBOL_GPL(ring_buffer_write);
  1512. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  1513. {
  1514. struct buffer_page *reader = cpu_buffer->reader_page;
  1515. struct buffer_page *head = cpu_buffer->head_page;
  1516. struct buffer_page *commit = cpu_buffer->commit_page;
  1517. return reader->read == rb_page_commit(reader) &&
  1518. (commit == reader ||
  1519. (commit == head &&
  1520. head->read == rb_page_commit(commit)));
  1521. }
  1522. /**
  1523. * ring_buffer_record_disable - stop all writes into the buffer
  1524. * @buffer: The ring buffer to stop writes to.
  1525. *
  1526. * This prevents all writes to the buffer. Any attempt to write
  1527. * to the buffer after this will fail and return NULL.
  1528. *
  1529. * The caller should call synchronize_sched() after this.
  1530. */
  1531. void ring_buffer_record_disable(struct ring_buffer *buffer)
  1532. {
  1533. atomic_inc(&buffer->record_disabled);
  1534. }
  1535. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  1536. /**
  1537. * ring_buffer_record_enable - enable writes to the buffer
  1538. * @buffer: The ring buffer to enable writes
  1539. *
  1540. * Note, multiple disables will need the same number of enables
  1541. * to truely enable the writing (much like preempt_disable).
  1542. */
  1543. void ring_buffer_record_enable(struct ring_buffer *buffer)
  1544. {
  1545. atomic_dec(&buffer->record_disabled);
  1546. }
  1547. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  1548. /**
  1549. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  1550. * @buffer: The ring buffer to stop writes to.
  1551. * @cpu: The CPU buffer to stop
  1552. *
  1553. * This prevents all writes to the buffer. Any attempt to write
  1554. * to the buffer after this will fail and return NULL.
  1555. *
  1556. * The caller should call synchronize_sched() after this.
  1557. */
  1558. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  1559. {
  1560. struct ring_buffer_per_cpu *cpu_buffer;
  1561. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1562. return;
  1563. cpu_buffer = buffer->buffers[cpu];
  1564. atomic_inc(&cpu_buffer->record_disabled);
  1565. }
  1566. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  1567. /**
  1568. * ring_buffer_record_enable_cpu - enable writes to the buffer
  1569. * @buffer: The ring buffer to enable writes
  1570. * @cpu: The CPU to enable.
  1571. *
  1572. * Note, multiple disables will need the same number of enables
  1573. * to truely enable the writing (much like preempt_disable).
  1574. */
  1575. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  1576. {
  1577. struct ring_buffer_per_cpu *cpu_buffer;
  1578. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1579. return;
  1580. cpu_buffer = buffer->buffers[cpu];
  1581. atomic_dec(&cpu_buffer->record_disabled);
  1582. }
  1583. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  1584. /**
  1585. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  1586. * @buffer: The ring buffer
  1587. * @cpu: The per CPU buffer to get the entries from.
  1588. */
  1589. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  1590. {
  1591. struct ring_buffer_per_cpu *cpu_buffer;
  1592. unsigned long ret;
  1593. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1594. return 0;
  1595. cpu_buffer = buffer->buffers[cpu];
  1596. ret = cpu_buffer->entries;
  1597. return ret;
  1598. }
  1599. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  1600. /**
  1601. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  1602. * @buffer: The ring buffer
  1603. * @cpu: The per CPU buffer to get the number of overruns from
  1604. */
  1605. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1606. {
  1607. struct ring_buffer_per_cpu *cpu_buffer;
  1608. unsigned long ret;
  1609. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1610. return 0;
  1611. cpu_buffer = buffer->buffers[cpu];
  1612. ret = cpu_buffer->overrun;
  1613. return ret;
  1614. }
  1615. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  1616. /**
  1617. * ring_buffer_entries - get the number of entries in a buffer
  1618. * @buffer: The ring buffer
  1619. *
  1620. * Returns the total number of entries in the ring buffer
  1621. * (all CPU entries)
  1622. */
  1623. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  1624. {
  1625. struct ring_buffer_per_cpu *cpu_buffer;
  1626. unsigned long entries = 0;
  1627. int cpu;
  1628. /* if you care about this being correct, lock the buffer */
  1629. for_each_buffer_cpu(buffer, cpu) {
  1630. cpu_buffer = buffer->buffers[cpu];
  1631. entries += cpu_buffer->entries;
  1632. }
  1633. return entries;
  1634. }
  1635. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  1636. /**
  1637. * ring_buffer_overrun_cpu - get the number of overruns in buffer
  1638. * @buffer: The ring buffer
  1639. *
  1640. * Returns the total number of overruns in the ring buffer
  1641. * (all CPU entries)
  1642. */
  1643. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  1644. {
  1645. struct ring_buffer_per_cpu *cpu_buffer;
  1646. unsigned long overruns = 0;
  1647. int cpu;
  1648. /* if you care about this being correct, lock the buffer */
  1649. for_each_buffer_cpu(buffer, cpu) {
  1650. cpu_buffer = buffer->buffers[cpu];
  1651. overruns += cpu_buffer->overrun;
  1652. }
  1653. return overruns;
  1654. }
  1655. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  1656. static void rb_iter_reset(struct ring_buffer_iter *iter)
  1657. {
  1658. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1659. /* Iterator usage is expected to have record disabled */
  1660. if (list_empty(&cpu_buffer->reader_page->list)) {
  1661. iter->head_page = cpu_buffer->head_page;
  1662. iter->head = cpu_buffer->head_page->read;
  1663. } else {
  1664. iter->head_page = cpu_buffer->reader_page;
  1665. iter->head = cpu_buffer->reader_page->read;
  1666. }
  1667. if (iter->head)
  1668. iter->read_stamp = cpu_buffer->read_stamp;
  1669. else
  1670. iter->read_stamp = iter->head_page->page->time_stamp;
  1671. }
  1672. /**
  1673. * ring_buffer_iter_reset - reset an iterator
  1674. * @iter: The iterator to reset
  1675. *
  1676. * Resets the iterator, so that it will start from the beginning
  1677. * again.
  1678. */
  1679. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  1680. {
  1681. struct ring_buffer_per_cpu *cpu_buffer;
  1682. unsigned long flags;
  1683. if (!iter)
  1684. return;
  1685. cpu_buffer = iter->cpu_buffer;
  1686. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1687. rb_iter_reset(iter);
  1688. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1689. }
  1690. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  1691. /**
  1692. * ring_buffer_iter_empty - check if an iterator has no more to read
  1693. * @iter: The iterator to check
  1694. */
  1695. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  1696. {
  1697. struct ring_buffer_per_cpu *cpu_buffer;
  1698. cpu_buffer = iter->cpu_buffer;
  1699. return iter->head_page == cpu_buffer->commit_page &&
  1700. iter->head == rb_commit_index(cpu_buffer);
  1701. }
  1702. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  1703. static void
  1704. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1705. struct ring_buffer_event *event)
  1706. {
  1707. u64 delta;
  1708. switch (event->type) {
  1709. case RINGBUF_TYPE_PADDING:
  1710. return;
  1711. case RINGBUF_TYPE_TIME_EXTEND:
  1712. delta = event->array[0];
  1713. delta <<= TS_SHIFT;
  1714. delta += event->time_delta;
  1715. cpu_buffer->read_stamp += delta;
  1716. return;
  1717. case RINGBUF_TYPE_TIME_STAMP:
  1718. /* FIXME: not implemented */
  1719. return;
  1720. case RINGBUF_TYPE_DATA:
  1721. cpu_buffer->read_stamp += event->time_delta;
  1722. return;
  1723. default:
  1724. BUG();
  1725. }
  1726. return;
  1727. }
  1728. static void
  1729. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  1730. struct ring_buffer_event *event)
  1731. {
  1732. u64 delta;
  1733. switch (event->type) {
  1734. case RINGBUF_TYPE_PADDING:
  1735. return;
  1736. case RINGBUF_TYPE_TIME_EXTEND:
  1737. delta = event->array[0];
  1738. delta <<= TS_SHIFT;
  1739. delta += event->time_delta;
  1740. iter->read_stamp += delta;
  1741. return;
  1742. case RINGBUF_TYPE_TIME_STAMP:
  1743. /* FIXME: not implemented */
  1744. return;
  1745. case RINGBUF_TYPE_DATA:
  1746. iter->read_stamp += event->time_delta;
  1747. return;
  1748. default:
  1749. BUG();
  1750. }
  1751. return;
  1752. }
  1753. static struct buffer_page *
  1754. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1755. {
  1756. struct buffer_page *reader = NULL;
  1757. unsigned long flags;
  1758. int nr_loops = 0;
  1759. local_irq_save(flags);
  1760. __raw_spin_lock(&cpu_buffer->lock);
  1761. again:
  1762. /*
  1763. * This should normally only loop twice. But because the
  1764. * start of the reader inserts an empty page, it causes
  1765. * a case where we will loop three times. There should be no
  1766. * reason to loop four times (that I know of).
  1767. */
  1768. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  1769. reader = NULL;
  1770. goto out;
  1771. }
  1772. reader = cpu_buffer->reader_page;
  1773. /* If there's more to read, return this page */
  1774. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  1775. goto out;
  1776. /* Never should we have an index greater than the size */
  1777. if (RB_WARN_ON(cpu_buffer,
  1778. cpu_buffer->reader_page->read > rb_page_size(reader)))
  1779. goto out;
  1780. /* check if we caught up to the tail */
  1781. reader = NULL;
  1782. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  1783. goto out;
  1784. /*
  1785. * Splice the empty reader page into the list around the head.
  1786. * Reset the reader page to size zero.
  1787. */
  1788. reader = cpu_buffer->head_page;
  1789. cpu_buffer->reader_page->list.next = reader->list.next;
  1790. cpu_buffer->reader_page->list.prev = reader->list.prev;
  1791. local_set(&cpu_buffer->reader_page->write, 0);
  1792. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1793. /* Make the reader page now replace the head */
  1794. reader->list.prev->next = &cpu_buffer->reader_page->list;
  1795. reader->list.next->prev = &cpu_buffer->reader_page->list;
  1796. /*
  1797. * If the tail is on the reader, then we must set the head
  1798. * to the inserted page, otherwise we set it one before.
  1799. */
  1800. cpu_buffer->head_page = cpu_buffer->reader_page;
  1801. if (cpu_buffer->commit_page != reader)
  1802. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  1803. /* Finally update the reader page to the new head */
  1804. cpu_buffer->reader_page = reader;
  1805. rb_reset_reader_page(cpu_buffer);
  1806. goto again;
  1807. out:
  1808. __raw_spin_unlock(&cpu_buffer->lock);
  1809. local_irq_restore(flags);
  1810. return reader;
  1811. }
  1812. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  1813. {
  1814. struct ring_buffer_event *event;
  1815. struct buffer_page *reader;
  1816. unsigned length;
  1817. reader = rb_get_reader_page(cpu_buffer);
  1818. /* This function should not be called when buffer is empty */
  1819. if (RB_WARN_ON(cpu_buffer, !reader))
  1820. return;
  1821. event = rb_reader_event(cpu_buffer);
  1822. if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
  1823. cpu_buffer->entries--;
  1824. rb_update_read_stamp(cpu_buffer, event);
  1825. length = rb_event_length(event);
  1826. cpu_buffer->reader_page->read += length;
  1827. }
  1828. static void rb_advance_iter(struct ring_buffer_iter *iter)
  1829. {
  1830. struct ring_buffer *buffer;
  1831. struct ring_buffer_per_cpu *cpu_buffer;
  1832. struct ring_buffer_event *event;
  1833. unsigned length;
  1834. cpu_buffer = iter->cpu_buffer;
  1835. buffer = cpu_buffer->buffer;
  1836. /*
  1837. * Check if we are at the end of the buffer.
  1838. */
  1839. if (iter->head >= rb_page_size(iter->head_page)) {
  1840. if (RB_WARN_ON(buffer,
  1841. iter->head_page == cpu_buffer->commit_page))
  1842. return;
  1843. rb_inc_iter(iter);
  1844. return;
  1845. }
  1846. event = rb_iter_head_event(iter);
  1847. length = rb_event_length(event);
  1848. /*
  1849. * This should not be called to advance the header if we are
  1850. * at the tail of the buffer.
  1851. */
  1852. if (RB_WARN_ON(cpu_buffer,
  1853. (iter->head_page == cpu_buffer->commit_page) &&
  1854. (iter->head + length > rb_commit_index(cpu_buffer))))
  1855. return;
  1856. rb_update_iter_read_stamp(iter, event);
  1857. iter->head += length;
  1858. /* check for end of page padding */
  1859. if ((iter->head >= rb_page_size(iter->head_page)) &&
  1860. (iter->head_page != cpu_buffer->commit_page))
  1861. rb_advance_iter(iter);
  1862. }
  1863. static struct ring_buffer_event *
  1864. rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1865. {
  1866. struct ring_buffer_per_cpu *cpu_buffer;
  1867. struct ring_buffer_event *event;
  1868. struct buffer_page *reader;
  1869. int nr_loops = 0;
  1870. cpu_buffer = buffer->buffers[cpu];
  1871. again:
  1872. /*
  1873. * We repeat when a timestamp is encountered. It is possible
  1874. * to get multiple timestamps from an interrupt entering just
  1875. * as one timestamp is about to be written. The max times
  1876. * that this can happen is the number of nested interrupts we
  1877. * can have. Nesting 10 deep of interrupts is clearly
  1878. * an anomaly.
  1879. */
  1880. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1881. return NULL;
  1882. reader = rb_get_reader_page(cpu_buffer);
  1883. if (!reader)
  1884. return NULL;
  1885. event = rb_reader_event(cpu_buffer);
  1886. switch (event->type) {
  1887. case RINGBUF_TYPE_PADDING:
  1888. if (rb_null_event(event))
  1889. RB_WARN_ON(cpu_buffer, 1);
  1890. /*
  1891. * Because the writer could be discarding every
  1892. * event it creates (which would probably be bad)
  1893. * if we were to go back to "again" then we may never
  1894. * catch up, and will trigger the warn on, or lock
  1895. * the box. Return the padding, and we will release
  1896. * the current locks, and try again.
  1897. */
  1898. rb_advance_reader(cpu_buffer);
  1899. return event;
  1900. case RINGBUF_TYPE_TIME_EXTEND:
  1901. /* Internal data, OK to advance */
  1902. rb_advance_reader(cpu_buffer);
  1903. goto again;
  1904. case RINGBUF_TYPE_TIME_STAMP:
  1905. /* FIXME: not implemented */
  1906. rb_advance_reader(cpu_buffer);
  1907. goto again;
  1908. case RINGBUF_TYPE_DATA:
  1909. if (ts) {
  1910. *ts = cpu_buffer->read_stamp + event->time_delta;
  1911. ring_buffer_normalize_time_stamp(buffer,
  1912. cpu_buffer->cpu, ts);
  1913. }
  1914. return event;
  1915. default:
  1916. BUG();
  1917. }
  1918. return NULL;
  1919. }
  1920. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  1921. static struct ring_buffer_event *
  1922. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  1923. {
  1924. struct ring_buffer *buffer;
  1925. struct ring_buffer_per_cpu *cpu_buffer;
  1926. struct ring_buffer_event *event;
  1927. int nr_loops = 0;
  1928. if (ring_buffer_iter_empty(iter))
  1929. return NULL;
  1930. cpu_buffer = iter->cpu_buffer;
  1931. buffer = cpu_buffer->buffer;
  1932. again:
  1933. /*
  1934. * We repeat when a timestamp is encountered. It is possible
  1935. * to get multiple timestamps from an interrupt entering just
  1936. * as one timestamp is about to be written. The max times
  1937. * that this can happen is the number of nested interrupts we
  1938. * can have. Nesting 10 deep of interrupts is clearly
  1939. * an anomaly.
  1940. */
  1941. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1942. return NULL;
  1943. if (rb_per_cpu_empty(cpu_buffer))
  1944. return NULL;
  1945. event = rb_iter_head_event(iter);
  1946. switch (event->type) {
  1947. case RINGBUF_TYPE_PADDING:
  1948. if (rb_null_event(event)) {
  1949. rb_inc_iter(iter);
  1950. goto again;
  1951. }
  1952. rb_advance_iter(iter);
  1953. return event;
  1954. case RINGBUF_TYPE_TIME_EXTEND:
  1955. /* Internal data, OK to advance */
  1956. rb_advance_iter(iter);
  1957. goto again;
  1958. case RINGBUF_TYPE_TIME_STAMP:
  1959. /* FIXME: not implemented */
  1960. rb_advance_iter(iter);
  1961. goto again;
  1962. case RINGBUF_TYPE_DATA:
  1963. if (ts) {
  1964. *ts = iter->read_stamp + event->time_delta;
  1965. ring_buffer_normalize_time_stamp(buffer,
  1966. cpu_buffer->cpu, ts);
  1967. }
  1968. return event;
  1969. default:
  1970. BUG();
  1971. }
  1972. return NULL;
  1973. }
  1974. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  1975. /**
  1976. * ring_buffer_peek - peek at the next event to be read
  1977. * @buffer: The ring buffer to read
  1978. * @cpu: The cpu to peak at
  1979. * @ts: The timestamp counter of this event.
  1980. *
  1981. * This will return the event that will be read next, but does
  1982. * not consume the data.
  1983. */
  1984. struct ring_buffer_event *
  1985. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1986. {
  1987. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  1988. struct ring_buffer_event *event;
  1989. unsigned long flags;
  1990. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1991. return NULL;
  1992. again:
  1993. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1994. event = rb_buffer_peek(buffer, cpu, ts);
  1995. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1996. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1997. cpu_relax();
  1998. goto again;
  1999. }
  2000. return event;
  2001. }
  2002. /**
  2003. * ring_buffer_iter_peek - peek at the next event to be read
  2004. * @iter: The ring buffer iterator
  2005. * @ts: The timestamp counter of this event.
  2006. *
  2007. * This will return the event that will be read next, but does
  2008. * not increment the iterator.
  2009. */
  2010. struct ring_buffer_event *
  2011. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2012. {
  2013. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2014. struct ring_buffer_event *event;
  2015. unsigned long flags;
  2016. again:
  2017. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2018. event = rb_iter_peek(iter, ts);
  2019. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2020. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2021. cpu_relax();
  2022. goto again;
  2023. }
  2024. return event;
  2025. }
  2026. /**
  2027. * ring_buffer_consume - return an event and consume it
  2028. * @buffer: The ring buffer to get the next event from
  2029. *
  2030. * Returns the next event in the ring buffer, and that event is consumed.
  2031. * Meaning, that sequential reads will keep returning a different event,
  2032. * and eventually empty the ring buffer if the producer is slower.
  2033. */
  2034. struct ring_buffer_event *
  2035. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2036. {
  2037. struct ring_buffer_per_cpu *cpu_buffer;
  2038. struct ring_buffer_event *event = NULL;
  2039. unsigned long flags;
  2040. again:
  2041. /* might be called in atomic */
  2042. preempt_disable();
  2043. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2044. goto out;
  2045. cpu_buffer = buffer->buffers[cpu];
  2046. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2047. event = rb_buffer_peek(buffer, cpu, ts);
  2048. if (!event)
  2049. goto out_unlock;
  2050. rb_advance_reader(cpu_buffer);
  2051. out_unlock:
  2052. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2053. out:
  2054. preempt_enable();
  2055. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2056. cpu_relax();
  2057. goto again;
  2058. }
  2059. return event;
  2060. }
  2061. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2062. /**
  2063. * ring_buffer_read_start - start a non consuming read of the buffer
  2064. * @buffer: The ring buffer to read from
  2065. * @cpu: The cpu buffer to iterate over
  2066. *
  2067. * This starts up an iteration through the buffer. It also disables
  2068. * the recording to the buffer until the reading is finished.
  2069. * This prevents the reading from being corrupted. This is not
  2070. * a consuming read, so a producer is not expected.
  2071. *
  2072. * Must be paired with ring_buffer_finish.
  2073. */
  2074. struct ring_buffer_iter *
  2075. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2076. {
  2077. struct ring_buffer_per_cpu *cpu_buffer;
  2078. struct ring_buffer_iter *iter;
  2079. unsigned long flags;
  2080. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2081. return NULL;
  2082. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2083. if (!iter)
  2084. return NULL;
  2085. cpu_buffer = buffer->buffers[cpu];
  2086. iter->cpu_buffer = cpu_buffer;
  2087. atomic_inc(&cpu_buffer->record_disabled);
  2088. synchronize_sched();
  2089. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2090. __raw_spin_lock(&cpu_buffer->lock);
  2091. rb_iter_reset(iter);
  2092. __raw_spin_unlock(&cpu_buffer->lock);
  2093. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2094. return iter;
  2095. }
  2096. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2097. /**
  2098. * ring_buffer_finish - finish reading the iterator of the buffer
  2099. * @iter: The iterator retrieved by ring_buffer_start
  2100. *
  2101. * This re-enables the recording to the buffer, and frees the
  2102. * iterator.
  2103. */
  2104. void
  2105. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2106. {
  2107. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2108. atomic_dec(&cpu_buffer->record_disabled);
  2109. kfree(iter);
  2110. }
  2111. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2112. /**
  2113. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2114. * @iter: The ring buffer iterator
  2115. * @ts: The time stamp of the event read.
  2116. *
  2117. * This reads the next event in the ring buffer and increments the iterator.
  2118. */
  2119. struct ring_buffer_event *
  2120. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2121. {
  2122. struct ring_buffer_event *event;
  2123. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2124. unsigned long flags;
  2125. again:
  2126. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2127. event = rb_iter_peek(iter, ts);
  2128. if (!event)
  2129. goto out;
  2130. rb_advance_iter(iter);
  2131. out:
  2132. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2133. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2134. cpu_relax();
  2135. goto again;
  2136. }
  2137. return event;
  2138. }
  2139. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2140. /**
  2141. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2142. * @buffer: The ring buffer.
  2143. */
  2144. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2145. {
  2146. return BUF_PAGE_SIZE * buffer->pages;
  2147. }
  2148. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2149. static void
  2150. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2151. {
  2152. cpu_buffer->head_page
  2153. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  2154. local_set(&cpu_buffer->head_page->write, 0);
  2155. local_set(&cpu_buffer->head_page->page->commit, 0);
  2156. cpu_buffer->head_page->read = 0;
  2157. cpu_buffer->tail_page = cpu_buffer->head_page;
  2158. cpu_buffer->commit_page = cpu_buffer->head_page;
  2159. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2160. local_set(&cpu_buffer->reader_page->write, 0);
  2161. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2162. cpu_buffer->reader_page->read = 0;
  2163. cpu_buffer->overrun = 0;
  2164. cpu_buffer->entries = 0;
  2165. cpu_buffer->write_stamp = 0;
  2166. cpu_buffer->read_stamp = 0;
  2167. }
  2168. /**
  2169. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2170. * @buffer: The ring buffer to reset a per cpu buffer of
  2171. * @cpu: The CPU buffer to be reset
  2172. */
  2173. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2174. {
  2175. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2176. unsigned long flags;
  2177. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2178. return;
  2179. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2180. __raw_spin_lock(&cpu_buffer->lock);
  2181. rb_reset_cpu(cpu_buffer);
  2182. __raw_spin_unlock(&cpu_buffer->lock);
  2183. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2184. }
  2185. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2186. /**
  2187. * ring_buffer_reset - reset a ring buffer
  2188. * @buffer: The ring buffer to reset all cpu buffers
  2189. */
  2190. void ring_buffer_reset(struct ring_buffer *buffer)
  2191. {
  2192. int cpu;
  2193. for_each_buffer_cpu(buffer, cpu)
  2194. ring_buffer_reset_cpu(buffer, cpu);
  2195. }
  2196. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2197. /**
  2198. * rind_buffer_empty - is the ring buffer empty?
  2199. * @buffer: The ring buffer to test
  2200. */
  2201. int ring_buffer_empty(struct ring_buffer *buffer)
  2202. {
  2203. struct ring_buffer_per_cpu *cpu_buffer;
  2204. int cpu;
  2205. /* yes this is racy, but if you don't like the race, lock the buffer */
  2206. for_each_buffer_cpu(buffer, cpu) {
  2207. cpu_buffer = buffer->buffers[cpu];
  2208. if (!rb_per_cpu_empty(cpu_buffer))
  2209. return 0;
  2210. }
  2211. return 1;
  2212. }
  2213. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2214. /**
  2215. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2216. * @buffer: The ring buffer
  2217. * @cpu: The CPU buffer to test
  2218. */
  2219. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2220. {
  2221. struct ring_buffer_per_cpu *cpu_buffer;
  2222. int ret;
  2223. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2224. return 1;
  2225. cpu_buffer = buffer->buffers[cpu];
  2226. ret = rb_per_cpu_empty(cpu_buffer);
  2227. return ret;
  2228. }
  2229. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2230. /**
  2231. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2232. * @buffer_a: One buffer to swap with
  2233. * @buffer_b: The other buffer to swap with
  2234. *
  2235. * This function is useful for tracers that want to take a "snapshot"
  2236. * of a CPU buffer and has another back up buffer lying around.
  2237. * it is expected that the tracer handles the cpu buffer not being
  2238. * used at the moment.
  2239. */
  2240. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2241. struct ring_buffer *buffer_b, int cpu)
  2242. {
  2243. struct ring_buffer_per_cpu *cpu_buffer_a;
  2244. struct ring_buffer_per_cpu *cpu_buffer_b;
  2245. int ret = -EINVAL;
  2246. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2247. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2248. goto out;
  2249. /* At least make sure the two buffers are somewhat the same */
  2250. if (buffer_a->pages != buffer_b->pages)
  2251. goto out;
  2252. ret = -EAGAIN;
  2253. if (ring_buffer_flags != RB_BUFFERS_ON)
  2254. goto out;
  2255. if (atomic_read(&buffer_a->record_disabled))
  2256. goto out;
  2257. if (atomic_read(&buffer_b->record_disabled))
  2258. goto out;
  2259. cpu_buffer_a = buffer_a->buffers[cpu];
  2260. cpu_buffer_b = buffer_b->buffers[cpu];
  2261. if (atomic_read(&cpu_buffer_a->record_disabled))
  2262. goto out;
  2263. if (atomic_read(&cpu_buffer_b->record_disabled))
  2264. goto out;
  2265. /*
  2266. * We can't do a synchronize_sched here because this
  2267. * function can be called in atomic context.
  2268. * Normally this will be called from the same CPU as cpu.
  2269. * If not it's up to the caller to protect this.
  2270. */
  2271. atomic_inc(&cpu_buffer_a->record_disabled);
  2272. atomic_inc(&cpu_buffer_b->record_disabled);
  2273. buffer_a->buffers[cpu] = cpu_buffer_b;
  2274. buffer_b->buffers[cpu] = cpu_buffer_a;
  2275. cpu_buffer_b->buffer = buffer_a;
  2276. cpu_buffer_a->buffer = buffer_b;
  2277. atomic_dec(&cpu_buffer_a->record_disabled);
  2278. atomic_dec(&cpu_buffer_b->record_disabled);
  2279. ret = 0;
  2280. out:
  2281. return ret;
  2282. }
  2283. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2284. static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
  2285. struct buffer_data_page *bpage,
  2286. unsigned int offset)
  2287. {
  2288. struct ring_buffer_event *event;
  2289. unsigned long head;
  2290. __raw_spin_lock(&cpu_buffer->lock);
  2291. for (head = offset; head < local_read(&bpage->commit);
  2292. head += rb_event_length(event)) {
  2293. event = __rb_data_page_index(bpage, head);
  2294. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  2295. return;
  2296. /* Only count data entries */
  2297. if (event->type != RINGBUF_TYPE_DATA)
  2298. continue;
  2299. cpu_buffer->entries--;
  2300. }
  2301. __raw_spin_unlock(&cpu_buffer->lock);
  2302. }
  2303. /**
  2304. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2305. * @buffer: the buffer to allocate for.
  2306. *
  2307. * This function is used in conjunction with ring_buffer_read_page.
  2308. * When reading a full page from the ring buffer, these functions
  2309. * can be used to speed up the process. The calling function should
  2310. * allocate a few pages first with this function. Then when it
  2311. * needs to get pages from the ring buffer, it passes the result
  2312. * of this function into ring_buffer_read_page, which will swap
  2313. * the page that was allocated, with the read page of the buffer.
  2314. *
  2315. * Returns:
  2316. * The page allocated, or NULL on error.
  2317. */
  2318. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  2319. {
  2320. struct buffer_data_page *bpage;
  2321. unsigned long addr;
  2322. addr = __get_free_page(GFP_KERNEL);
  2323. if (!addr)
  2324. return NULL;
  2325. bpage = (void *)addr;
  2326. rb_init_page(bpage);
  2327. return bpage;
  2328. }
  2329. /**
  2330. * ring_buffer_free_read_page - free an allocated read page
  2331. * @buffer: the buffer the page was allocate for
  2332. * @data: the page to free
  2333. *
  2334. * Free a page allocated from ring_buffer_alloc_read_page.
  2335. */
  2336. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  2337. {
  2338. free_page((unsigned long)data);
  2339. }
  2340. /**
  2341. * ring_buffer_read_page - extract a page from the ring buffer
  2342. * @buffer: buffer to extract from
  2343. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  2344. * @len: amount to extract
  2345. * @cpu: the cpu of the buffer to extract
  2346. * @full: should the extraction only happen when the page is full.
  2347. *
  2348. * This function will pull out a page from the ring buffer and consume it.
  2349. * @data_page must be the address of the variable that was returned
  2350. * from ring_buffer_alloc_read_page. This is because the page might be used
  2351. * to swap with a page in the ring buffer.
  2352. *
  2353. * for example:
  2354. * rpage = ring_buffer_alloc_read_page(buffer);
  2355. * if (!rpage)
  2356. * return error;
  2357. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  2358. * if (ret >= 0)
  2359. * process_page(rpage, ret);
  2360. *
  2361. * When @full is set, the function will not return true unless
  2362. * the writer is off the reader page.
  2363. *
  2364. * Note: it is up to the calling functions to handle sleeps and wakeups.
  2365. * The ring buffer can be used anywhere in the kernel and can not
  2366. * blindly call wake_up. The layer that uses the ring buffer must be
  2367. * responsible for that.
  2368. *
  2369. * Returns:
  2370. * >=0 if data has been transferred, returns the offset of consumed data.
  2371. * <0 if no data has been transferred.
  2372. */
  2373. int ring_buffer_read_page(struct ring_buffer *buffer,
  2374. void **data_page, size_t len, int cpu, int full)
  2375. {
  2376. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2377. struct ring_buffer_event *event;
  2378. struct buffer_data_page *bpage;
  2379. struct buffer_page *reader;
  2380. unsigned long flags;
  2381. unsigned int commit;
  2382. unsigned int read;
  2383. u64 save_timestamp;
  2384. int ret = -1;
  2385. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2386. goto out;
  2387. /*
  2388. * If len is not big enough to hold the page header, then
  2389. * we can not copy anything.
  2390. */
  2391. if (len <= BUF_PAGE_HDR_SIZE)
  2392. goto out;
  2393. len -= BUF_PAGE_HDR_SIZE;
  2394. if (!data_page)
  2395. goto out;
  2396. bpage = *data_page;
  2397. if (!bpage)
  2398. goto out;
  2399. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2400. reader = rb_get_reader_page(cpu_buffer);
  2401. if (!reader)
  2402. goto out_unlock;
  2403. event = rb_reader_event(cpu_buffer);
  2404. read = reader->read;
  2405. commit = rb_page_commit(reader);
  2406. /*
  2407. * If this page has been partially read or
  2408. * if len is not big enough to read the rest of the page or
  2409. * a writer is still on the page, then
  2410. * we must copy the data from the page to the buffer.
  2411. * Otherwise, we can simply swap the page with the one passed in.
  2412. */
  2413. if (read || (len < (commit - read)) ||
  2414. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  2415. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  2416. unsigned int rpos = read;
  2417. unsigned int pos = 0;
  2418. unsigned int size;
  2419. if (full)
  2420. goto out_unlock;
  2421. if (len > (commit - read))
  2422. len = (commit - read);
  2423. size = rb_event_length(event);
  2424. if (len < size)
  2425. goto out_unlock;
  2426. /* save the current timestamp, since the user will need it */
  2427. save_timestamp = cpu_buffer->read_stamp;
  2428. /* Need to copy one event at a time */
  2429. do {
  2430. memcpy(bpage->data + pos, rpage->data + rpos, size);
  2431. len -= size;
  2432. rb_advance_reader(cpu_buffer);
  2433. rpos = reader->read;
  2434. pos += size;
  2435. event = rb_reader_event(cpu_buffer);
  2436. size = rb_event_length(event);
  2437. } while (len > size);
  2438. /* update bpage */
  2439. local_set(&bpage->commit, pos);
  2440. bpage->time_stamp = save_timestamp;
  2441. /* we copied everything to the beginning */
  2442. read = 0;
  2443. } else {
  2444. /* swap the pages */
  2445. rb_init_page(bpage);
  2446. bpage = reader->page;
  2447. reader->page = *data_page;
  2448. local_set(&reader->write, 0);
  2449. reader->read = 0;
  2450. *data_page = bpage;
  2451. /* update the entry counter */
  2452. rb_remove_entries(cpu_buffer, bpage, read);
  2453. }
  2454. ret = read;
  2455. out_unlock:
  2456. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2457. out:
  2458. return ret;
  2459. }
  2460. static ssize_t
  2461. rb_simple_read(struct file *filp, char __user *ubuf,
  2462. size_t cnt, loff_t *ppos)
  2463. {
  2464. unsigned long *p = filp->private_data;
  2465. char buf[64];
  2466. int r;
  2467. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  2468. r = sprintf(buf, "permanently disabled\n");
  2469. else
  2470. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  2471. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2472. }
  2473. static ssize_t
  2474. rb_simple_write(struct file *filp, const char __user *ubuf,
  2475. size_t cnt, loff_t *ppos)
  2476. {
  2477. unsigned long *p = filp->private_data;
  2478. char buf[64];
  2479. unsigned long val;
  2480. int ret;
  2481. if (cnt >= sizeof(buf))
  2482. return -EINVAL;
  2483. if (copy_from_user(&buf, ubuf, cnt))
  2484. return -EFAULT;
  2485. buf[cnt] = 0;
  2486. ret = strict_strtoul(buf, 10, &val);
  2487. if (ret < 0)
  2488. return ret;
  2489. if (val)
  2490. set_bit(RB_BUFFERS_ON_BIT, p);
  2491. else
  2492. clear_bit(RB_BUFFERS_ON_BIT, p);
  2493. (*ppos)++;
  2494. return cnt;
  2495. }
  2496. static const struct file_operations rb_simple_fops = {
  2497. .open = tracing_open_generic,
  2498. .read = rb_simple_read,
  2499. .write = rb_simple_write,
  2500. };
  2501. static __init int rb_init_debugfs(void)
  2502. {
  2503. struct dentry *d_tracer;
  2504. d_tracer = tracing_init_dentry();
  2505. trace_create_file("tracing_on", 0644, d_tracer,
  2506. &ring_buffer_flags, &rb_simple_fops);
  2507. return 0;
  2508. }
  2509. fs_initcall(rb_init_debugfs);
  2510. #ifdef CONFIG_HOTPLUG_CPU
  2511. static int rb_cpu_notify(struct notifier_block *self,
  2512. unsigned long action, void *hcpu)
  2513. {
  2514. struct ring_buffer *buffer =
  2515. container_of(self, struct ring_buffer, cpu_notify);
  2516. long cpu = (long)hcpu;
  2517. switch (action) {
  2518. case CPU_UP_PREPARE:
  2519. case CPU_UP_PREPARE_FROZEN:
  2520. if (cpu_isset(cpu, *buffer->cpumask))
  2521. return NOTIFY_OK;
  2522. buffer->buffers[cpu] =
  2523. rb_allocate_cpu_buffer(buffer, cpu);
  2524. if (!buffer->buffers[cpu]) {
  2525. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  2526. cpu);
  2527. return NOTIFY_OK;
  2528. }
  2529. smp_wmb();
  2530. cpu_set(cpu, *buffer->cpumask);
  2531. break;
  2532. case CPU_DOWN_PREPARE:
  2533. case CPU_DOWN_PREPARE_FROZEN:
  2534. /*
  2535. * Do nothing.
  2536. * If we were to free the buffer, then the user would
  2537. * lose any trace that was in the buffer.
  2538. */
  2539. break;
  2540. default:
  2541. break;
  2542. }
  2543. return NOTIFY_OK;
  2544. }
  2545. #endif