ring_buffer.c 78 KB

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