ring_buffer.c 75 KB

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