ring_buffer.c 76 KB

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