ring_buffer.c 73 KB

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