ring_buffer.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  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/kmemcheck.h>
  14. #include <linux/module.h>
  15. #include <linux/percpu.h>
  16. #include <linux/mutex.h>
  17. #include <linux/init.h>
  18. #include <linux/hash.h>
  19. #include <linux/list.h>
  20. #include <linux/cpu.h>
  21. #include <linux/fs.h>
  22. #include "trace.h"
  23. /*
  24. * The ring buffer header is special. We must manually up keep it.
  25. */
  26. int ring_buffer_print_entry_header(struct trace_seq *s)
  27. {
  28. int ret;
  29. ret = trace_seq_printf(s, "# compressed entry header\n");
  30. ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
  31. ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
  32. ret = trace_seq_printf(s, "\tarray : 32 bits\n");
  33. ret = trace_seq_printf(s, "\n");
  34. ret = trace_seq_printf(s, "\tpadding : type == %d\n",
  35. RINGBUF_TYPE_PADDING);
  36. ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
  37. RINGBUF_TYPE_TIME_EXTEND);
  38. ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
  39. RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  40. return ret;
  41. }
  42. /*
  43. * The ring buffer is made up of a list of pages. A separate list of pages is
  44. * allocated for each CPU. A writer may only write to a buffer that is
  45. * associated with the CPU it is currently executing on. A reader may read
  46. * from any per cpu buffer.
  47. *
  48. * The reader is special. For each per cpu buffer, the reader has its own
  49. * reader page. When a reader has read the entire reader page, this reader
  50. * page is swapped with another page in the ring buffer.
  51. *
  52. * Now, as long as the writer is off the reader page, the reader can do what
  53. * ever it wants with that page. The writer will never write to that page
  54. * again (as long as it is out of the ring buffer).
  55. *
  56. * Here's some silly ASCII art.
  57. *
  58. * +------+
  59. * |reader| RING BUFFER
  60. * |page |
  61. * +------+ +---+ +---+ +---+
  62. * | |-->| |-->| |
  63. * +---+ +---+ +---+
  64. * ^ |
  65. * | |
  66. * +---------------+
  67. *
  68. *
  69. * +------+
  70. * |reader| RING BUFFER
  71. * |page |------------------v
  72. * +------+ +---+ +---+ +---+
  73. * | |-->| |-->| |
  74. * +---+ +---+ +---+
  75. * ^ |
  76. * | |
  77. * +---------------+
  78. *
  79. *
  80. * +------+
  81. * |reader| RING BUFFER
  82. * |page |------------------v
  83. * +------+ +---+ +---+ +---+
  84. * ^ | |-->| |-->| |
  85. * | +---+ +---+ +---+
  86. * | |
  87. * | |
  88. * +------------------------------+
  89. *
  90. *
  91. * +------+
  92. * |buffer| RING BUFFER
  93. * |page |------------------v
  94. * +------+ +---+ +---+ +---+
  95. * ^ | | | |-->| |
  96. * | New +---+ +---+ +---+
  97. * | Reader------^ |
  98. * | page |
  99. * +------------------------------+
  100. *
  101. *
  102. * After we make this swap, the reader can hand this page off to the splice
  103. * code and be done with it. It can even allocate a new page if it needs to
  104. * and swap that into the ring buffer.
  105. *
  106. * We will be using cmpxchg soon to make all this lockless.
  107. *
  108. */
  109. /*
  110. * A fast way to enable or disable all ring buffers is to
  111. * call tracing_on or tracing_off. Turning off the ring buffers
  112. * prevents all ring buffers from being recorded to.
  113. * Turning this switch on, makes it OK to write to the
  114. * ring buffer, if the ring buffer is enabled itself.
  115. *
  116. * There's three layers that must be on in order to write
  117. * to the ring buffer.
  118. *
  119. * 1) This global flag must be set.
  120. * 2) The ring buffer must be enabled for recording.
  121. * 3) The per cpu buffer must be enabled for recording.
  122. *
  123. * In case of an anomaly, this global flag has a bit set that
  124. * will permantly disable all ring buffers.
  125. */
  126. /*
  127. * Global flag to disable all recording to ring buffers
  128. * This has two bits: ON, DISABLED
  129. *
  130. * ON DISABLED
  131. * ---- ----------
  132. * 0 0 : ring buffers are off
  133. * 1 0 : ring buffers are on
  134. * X 1 : ring buffers are permanently disabled
  135. */
  136. enum {
  137. RB_BUFFERS_ON_BIT = 0,
  138. RB_BUFFERS_DISABLED_BIT = 1,
  139. };
  140. enum {
  141. RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
  142. RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
  143. };
  144. static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
  145. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  146. /**
  147. * tracing_on - enable all tracing buffers
  148. *
  149. * This function enables all tracing buffers that may have been
  150. * disabled with tracing_off.
  151. */
  152. void tracing_on(void)
  153. {
  154. set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  155. }
  156. EXPORT_SYMBOL_GPL(tracing_on);
  157. /**
  158. * tracing_off - turn off all tracing buffers
  159. *
  160. * This function stops all tracing buffers from recording data.
  161. * It does not disable any overhead the tracers themselves may
  162. * be causing. This function simply causes all recording to
  163. * the ring buffers to fail.
  164. */
  165. void tracing_off(void)
  166. {
  167. clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  168. }
  169. EXPORT_SYMBOL_GPL(tracing_off);
  170. /**
  171. * tracing_off_permanent - permanently disable ring buffers
  172. *
  173. * This function, once called, will disable all ring buffers
  174. * permanently.
  175. */
  176. void tracing_off_permanent(void)
  177. {
  178. set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
  179. }
  180. /**
  181. * tracing_is_on - show state of ring buffers enabled
  182. */
  183. int tracing_is_on(void)
  184. {
  185. return ring_buffer_flags == RB_BUFFERS_ON;
  186. }
  187. EXPORT_SYMBOL_GPL(tracing_is_on);
  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 && !event->time_delta;
  201. }
  202. static void rb_event_set_padding(struct ring_buffer_event *event)
  203. {
  204. /* padding has a NULL time_delta */
  205. event->type_len = RINGBUF_TYPE_PADDING;
  206. event->time_delta = 0;
  207. }
  208. static unsigned
  209. rb_event_data_length(struct ring_buffer_event *event)
  210. {
  211. unsigned length;
  212. if (event->type_len)
  213. length = event->type_len * RB_ALIGNMENT;
  214. else
  215. length = event->array[0];
  216. return length + RB_EVNT_HDR_SIZE;
  217. }
  218. /* inline for ring buffer fast paths */
  219. static unsigned
  220. rb_event_length(struct ring_buffer_event *event)
  221. {
  222. switch (event->type_len) {
  223. case RINGBUF_TYPE_PADDING:
  224. if (rb_null_event(event))
  225. /* undefined */
  226. return -1;
  227. return event->array[0] + RB_EVNT_HDR_SIZE;
  228. case RINGBUF_TYPE_TIME_EXTEND:
  229. return RB_LEN_TIME_EXTEND;
  230. case RINGBUF_TYPE_TIME_STAMP:
  231. return RB_LEN_TIME_STAMP;
  232. case RINGBUF_TYPE_DATA:
  233. return rb_event_data_length(event);
  234. default:
  235. BUG();
  236. }
  237. /* not hit */
  238. return 0;
  239. }
  240. /**
  241. * ring_buffer_event_length - return the length of the event
  242. * @event: the event to get the length of
  243. */
  244. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  245. {
  246. unsigned length = rb_event_length(event);
  247. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  248. return length;
  249. length -= RB_EVNT_HDR_SIZE;
  250. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  251. length -= sizeof(event->array[0]);
  252. return length;
  253. }
  254. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  255. /* inline for ring buffer fast paths */
  256. static void *
  257. rb_event_data(struct ring_buffer_event *event)
  258. {
  259. BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  260. /* If length is in len field, then array[0] has the data */
  261. if (event->type_len)
  262. return (void *)&event->array[0];
  263. /* Otherwise length is in array[0] and array[1] has the data */
  264. return (void *)&event->array[1];
  265. }
  266. /**
  267. * ring_buffer_event_data - return the data of the event
  268. * @event: the event to get the data from
  269. */
  270. void *ring_buffer_event_data(struct ring_buffer_event *event)
  271. {
  272. return rb_event_data(event);
  273. }
  274. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  275. #define for_each_buffer_cpu(buffer, cpu) \
  276. for_each_cpu(cpu, buffer->cpumask)
  277. #define TS_SHIFT 27
  278. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  279. #define TS_DELTA_TEST (~TS_MASK)
  280. struct buffer_data_page {
  281. u64 time_stamp; /* page time stamp */
  282. local_t commit; /* write committed index */
  283. unsigned char data[]; /* data of buffer page */
  284. };
  285. /*
  286. * Note, the buffer_page list must be first. The buffer pages
  287. * are allocated in cache lines, which means that each buffer
  288. * page will be at the beginning of a cache line, and thus
  289. * the least significant bits will be zero. We use this to
  290. * add flags in the list struct pointers, to make the ring buffer
  291. * lockless.
  292. */
  293. struct buffer_page {
  294. struct list_head list; /* list of buffer pages */
  295. local_t write; /* index for next write */
  296. unsigned read; /* index for next read */
  297. local_t entries; /* entries on this page */
  298. struct buffer_data_page *page; /* Actual data page */
  299. };
  300. /*
  301. * The buffer page counters, write and entries, must be reset
  302. * atomically when crossing page boundaries. To synchronize this
  303. * update, two counters are inserted into the number. One is
  304. * the actual counter for the write position or count on the page.
  305. *
  306. * The other is a counter of updaters. Before an update happens
  307. * the update partition of the counter is incremented. This will
  308. * allow the updater to update the counter atomically.
  309. *
  310. * The counter is 20 bits, and the state data is 12.
  311. */
  312. #define RB_WRITE_MASK 0xfffff
  313. #define RB_WRITE_INTCNT (1 << 20)
  314. static void rb_init_page(struct buffer_data_page *bpage)
  315. {
  316. local_set(&bpage->commit, 0);
  317. }
  318. /**
  319. * ring_buffer_page_len - the size of data on the page.
  320. * @page: The page to read
  321. *
  322. * Returns the amount of data on the page, including buffer page header.
  323. */
  324. size_t ring_buffer_page_len(void *page)
  325. {
  326. return local_read(&((struct buffer_data_page *)page)->commit)
  327. + BUF_PAGE_HDR_SIZE;
  328. }
  329. /*
  330. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  331. * this issue out.
  332. */
  333. static void free_buffer_page(struct buffer_page *bpage)
  334. {
  335. free_page((unsigned long)bpage->page);
  336. kfree(bpage);
  337. }
  338. /*
  339. * We need to fit the time_stamp delta into 27 bits.
  340. */
  341. static inline int test_time_stamp(u64 delta)
  342. {
  343. if (delta & TS_DELTA_TEST)
  344. return 1;
  345. return 0;
  346. }
  347. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  348. /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
  349. #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
  350. /* Max number of timestamps that can fit on a page */
  351. #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
  352. int ring_buffer_print_page_header(struct trace_seq *s)
  353. {
  354. struct buffer_data_page field;
  355. int ret;
  356. ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  357. "offset:0;\tsize:%u;\tsigned:%u;\n",
  358. (unsigned int)sizeof(field.time_stamp),
  359. (unsigned int)is_signed_type(u64));
  360. ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
  361. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  362. (unsigned int)offsetof(typeof(field), commit),
  363. (unsigned int)sizeof(field.commit),
  364. (unsigned int)is_signed_type(long));
  365. ret = trace_seq_printf(s, "\tfield: char data;\t"
  366. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  367. (unsigned int)offsetof(typeof(field), data),
  368. (unsigned int)BUF_PAGE_SIZE,
  369. (unsigned int)is_signed_type(char));
  370. return ret;
  371. }
  372. /*
  373. * head_page == tail_page && head == tail then buffer is empty.
  374. */
  375. struct ring_buffer_per_cpu {
  376. int cpu;
  377. struct ring_buffer *buffer;
  378. spinlock_t reader_lock; /* serialize readers */
  379. raw_spinlock_t lock;
  380. struct lock_class_key lock_key;
  381. struct list_head *pages;
  382. struct buffer_page *head_page; /* read from head */
  383. struct buffer_page *tail_page; /* write to tail */
  384. struct buffer_page *commit_page; /* committed pages */
  385. struct buffer_page *reader_page;
  386. local_t commit_overrun;
  387. local_t overrun;
  388. local_t entries;
  389. local_t committing;
  390. local_t commits;
  391. unsigned long read;
  392. u64 write_stamp;
  393. u64 read_stamp;
  394. atomic_t record_disabled;
  395. };
  396. struct ring_buffer {
  397. unsigned pages;
  398. unsigned flags;
  399. int cpus;
  400. atomic_t record_disabled;
  401. cpumask_var_t cpumask;
  402. struct lock_class_key *reader_lock_key;
  403. struct mutex mutex;
  404. struct ring_buffer_per_cpu **buffers;
  405. #ifdef CONFIG_HOTPLUG_CPU
  406. struct notifier_block cpu_notify;
  407. #endif
  408. u64 (*clock)(void);
  409. };
  410. struct ring_buffer_iter {
  411. struct ring_buffer_per_cpu *cpu_buffer;
  412. unsigned long head;
  413. struct buffer_page *head_page;
  414. u64 read_stamp;
  415. };
  416. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  417. #define RB_WARN_ON(b, cond) \
  418. ({ \
  419. int _____ret = unlikely(cond); \
  420. if (_____ret) { \
  421. if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
  422. struct ring_buffer_per_cpu *__b = \
  423. (void *)b; \
  424. atomic_inc(&__b->buffer->record_disabled); \
  425. } else \
  426. atomic_inc(&b->record_disabled); \
  427. WARN_ON(1); \
  428. } \
  429. _____ret; \
  430. })
  431. /* Up this if you want to test the TIME_EXTENTS and normalization */
  432. #define DEBUG_SHIFT 0
  433. static inline u64 rb_time_stamp(struct ring_buffer *buffer)
  434. {
  435. /* shift to debug/test normalization and TIME_EXTENTS */
  436. return buffer->clock() << DEBUG_SHIFT;
  437. }
  438. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  439. {
  440. u64 time;
  441. preempt_disable_notrace();
  442. time = rb_time_stamp(buffer);
  443. preempt_enable_no_resched_notrace();
  444. return time;
  445. }
  446. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  447. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  448. int cpu, u64 *ts)
  449. {
  450. /* Just stupid testing the normalize function and deltas */
  451. *ts >>= DEBUG_SHIFT;
  452. }
  453. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  454. /*
  455. * Making the ring buffer lockless makes things tricky.
  456. * Although writes only happen on the CPU that they are on,
  457. * and they only need to worry about interrupts. Reads can
  458. * happen on any CPU.
  459. *
  460. * The reader page is always off the ring buffer, but when the
  461. * reader finishes with a page, it needs to swap its page with
  462. * a new one from the buffer. The reader needs to take from
  463. * the head (writes go to the tail). But if a writer is in overwrite
  464. * mode and wraps, it must push the head page forward.
  465. *
  466. * Here lies the problem.
  467. *
  468. * The reader must be careful to replace only the head page, and
  469. * not another one. As described at the top of the file in the
  470. * ASCII art, the reader sets its old page to point to the next
  471. * page after head. It then sets the page after head to point to
  472. * the old reader page. But if the writer moves the head page
  473. * during this operation, the reader could end up with the tail.
  474. *
  475. * We use cmpxchg to help prevent this race. We also do something
  476. * special with the page before head. We set the LSB to 1.
  477. *
  478. * When the writer must push the page forward, it will clear the
  479. * bit that points to the head page, move the head, and then set
  480. * the bit that points to the new head page.
  481. *
  482. * We also don't want an interrupt coming in and moving the head
  483. * page on another writer. Thus we use the second LSB to catch
  484. * that too. Thus:
  485. *
  486. * head->list->prev->next bit 1 bit 0
  487. * ------- -------
  488. * Normal page 0 0
  489. * Points to head page 0 1
  490. * New head page 1 0
  491. *
  492. * Note we can not trust the prev pointer of the head page, because:
  493. *
  494. * +----+ +-----+ +-----+
  495. * | |------>| T |---X--->| N |
  496. * | |<------| | | |
  497. * +----+ +-----+ +-----+
  498. * ^ ^ |
  499. * | +-----+ | |
  500. * +----------| R |----------+ |
  501. * | |<-----------+
  502. * +-----+
  503. *
  504. * Key: ---X--> HEAD flag set in pointer
  505. * T Tail page
  506. * R Reader page
  507. * N Next page
  508. *
  509. * (see __rb_reserve_next() to see where this happens)
  510. *
  511. * What the above shows is that the reader just swapped out
  512. * the reader page with a page in the buffer, but before it
  513. * could make the new header point back to the new page added
  514. * it was preempted by a writer. The writer moved forward onto
  515. * the new page added by the reader and is about to move forward
  516. * again.
  517. *
  518. * You can see, it is legitimate for the previous pointer of
  519. * the head (or any page) not to point back to itself. But only
  520. * temporarially.
  521. */
  522. #define RB_PAGE_NORMAL 0UL
  523. #define RB_PAGE_HEAD 1UL
  524. #define RB_PAGE_UPDATE 2UL
  525. #define RB_FLAG_MASK 3UL
  526. /* PAGE_MOVED is not part of the mask */
  527. #define RB_PAGE_MOVED 4UL
  528. /*
  529. * rb_list_head - remove any bit
  530. */
  531. static struct list_head *rb_list_head(struct list_head *list)
  532. {
  533. unsigned long val = (unsigned long)list;
  534. return (struct list_head *)(val & ~RB_FLAG_MASK);
  535. }
  536. /*
  537. * rb_is_head_page - test if the given page is the head page
  538. *
  539. * Because the reader may move the head_page pointer, we can
  540. * not trust what the head page is (it may be pointing to
  541. * the reader page). But if the next page is a header page,
  542. * its flags will be non zero.
  543. */
  544. static int inline
  545. rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  546. struct buffer_page *page, struct list_head *list)
  547. {
  548. unsigned long val;
  549. val = (unsigned long)list->next;
  550. if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
  551. return RB_PAGE_MOVED;
  552. return val & RB_FLAG_MASK;
  553. }
  554. /*
  555. * rb_is_reader_page
  556. *
  557. * The unique thing about the reader page, is that, if the
  558. * writer is ever on it, the previous pointer never points
  559. * back to the reader page.
  560. */
  561. static int rb_is_reader_page(struct buffer_page *page)
  562. {
  563. struct list_head *list = page->list.prev;
  564. return rb_list_head(list->next) != &page->list;
  565. }
  566. /*
  567. * rb_set_list_to_head - set a list_head to be pointing to head.
  568. */
  569. static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
  570. struct list_head *list)
  571. {
  572. unsigned long *ptr;
  573. ptr = (unsigned long *)&list->next;
  574. *ptr |= RB_PAGE_HEAD;
  575. *ptr &= ~RB_PAGE_UPDATE;
  576. }
  577. /*
  578. * rb_head_page_activate - sets up head page
  579. */
  580. static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
  581. {
  582. struct buffer_page *head;
  583. head = cpu_buffer->head_page;
  584. if (!head)
  585. return;
  586. /*
  587. * Set the previous list pointer to have the HEAD flag.
  588. */
  589. rb_set_list_to_head(cpu_buffer, head->list.prev);
  590. }
  591. static void rb_list_head_clear(struct list_head *list)
  592. {
  593. unsigned long *ptr = (unsigned long *)&list->next;
  594. *ptr &= ~RB_FLAG_MASK;
  595. }
  596. /*
  597. * rb_head_page_dactivate - clears head page ptr (for free list)
  598. */
  599. static void
  600. rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
  601. {
  602. struct list_head *hd;
  603. /* Go through the whole list and clear any pointers found. */
  604. rb_list_head_clear(cpu_buffer->pages);
  605. list_for_each(hd, cpu_buffer->pages)
  606. rb_list_head_clear(hd);
  607. }
  608. static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
  609. struct buffer_page *head,
  610. struct buffer_page *prev,
  611. int old_flag, int new_flag)
  612. {
  613. struct list_head *list;
  614. unsigned long val = (unsigned long)&head->list;
  615. unsigned long ret;
  616. list = &prev->list;
  617. val &= ~RB_FLAG_MASK;
  618. ret = cmpxchg((unsigned long *)&list->next,
  619. val | old_flag, val | new_flag);
  620. /* check if the reader took the page */
  621. if ((ret & ~RB_FLAG_MASK) != val)
  622. return RB_PAGE_MOVED;
  623. return ret & RB_FLAG_MASK;
  624. }
  625. static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
  626. struct buffer_page *head,
  627. struct buffer_page *prev,
  628. int old_flag)
  629. {
  630. return rb_head_page_set(cpu_buffer, head, prev,
  631. old_flag, RB_PAGE_UPDATE);
  632. }
  633. static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
  634. struct buffer_page *head,
  635. struct buffer_page *prev,
  636. int old_flag)
  637. {
  638. return rb_head_page_set(cpu_buffer, head, prev,
  639. old_flag, RB_PAGE_HEAD);
  640. }
  641. static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
  642. struct buffer_page *head,
  643. struct buffer_page *prev,
  644. int old_flag)
  645. {
  646. return rb_head_page_set(cpu_buffer, head, prev,
  647. old_flag, RB_PAGE_NORMAL);
  648. }
  649. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  650. struct buffer_page **bpage)
  651. {
  652. struct list_head *p = rb_list_head((*bpage)->list.next);
  653. *bpage = list_entry(p, struct buffer_page, list);
  654. }
  655. static struct buffer_page *
  656. rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
  657. {
  658. struct buffer_page *head;
  659. struct buffer_page *page;
  660. struct list_head *list;
  661. int i;
  662. if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
  663. return NULL;
  664. /* sanity check */
  665. list = cpu_buffer->pages;
  666. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
  667. return NULL;
  668. page = head = cpu_buffer->head_page;
  669. /*
  670. * It is possible that the writer moves the header behind
  671. * where we started, and we miss in one loop.
  672. * A second loop should grab the header, but we'll do
  673. * three loops just because I'm paranoid.
  674. */
  675. for (i = 0; i < 3; i++) {
  676. do {
  677. if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
  678. cpu_buffer->head_page = page;
  679. return page;
  680. }
  681. rb_inc_page(cpu_buffer, &page);
  682. } while (page != head);
  683. }
  684. RB_WARN_ON(cpu_buffer, 1);
  685. return NULL;
  686. }
  687. static int rb_head_page_replace(struct buffer_page *old,
  688. struct buffer_page *new)
  689. {
  690. unsigned long *ptr = (unsigned long *)&old->list.prev->next;
  691. unsigned long val;
  692. unsigned long ret;
  693. val = *ptr & ~RB_FLAG_MASK;
  694. val |= RB_PAGE_HEAD;
  695. ret = cmpxchg(ptr, val, (unsigned long)&new->list);
  696. return ret == val;
  697. }
  698. /*
  699. * rb_tail_page_update - move the tail page forward
  700. *
  701. * Returns 1 if moved tail page, 0 if someone else did.
  702. */
  703. static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
  704. struct buffer_page *tail_page,
  705. struct buffer_page *next_page)
  706. {
  707. struct buffer_page *old_tail;
  708. unsigned long old_entries;
  709. unsigned long old_write;
  710. int ret = 0;
  711. /*
  712. * The tail page now needs to be moved forward.
  713. *
  714. * We need to reset the tail page, but without messing
  715. * with possible erasing of data brought in by interrupts
  716. * that have moved the tail page and are currently on it.
  717. *
  718. * We add a counter to the write field to denote this.
  719. */
  720. old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
  721. old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
  722. /*
  723. * Just make sure we have seen our old_write and synchronize
  724. * with any interrupts that come in.
  725. */
  726. barrier();
  727. /*
  728. * If the tail page is still the same as what we think
  729. * it is, then it is up to us to update the tail
  730. * pointer.
  731. */
  732. if (tail_page == cpu_buffer->tail_page) {
  733. /* Zero the write counter */
  734. unsigned long val = old_write & ~RB_WRITE_MASK;
  735. unsigned long eval = old_entries & ~RB_WRITE_MASK;
  736. /*
  737. * This will only succeed if an interrupt did
  738. * not come in and change it. In which case, we
  739. * do not want to modify it.
  740. *
  741. * We add (void) to let the compiler know that we do not care
  742. * about the return value of these functions. We use the
  743. * cmpxchg to only update if an interrupt did not already
  744. * do it for us. If the cmpxchg fails, we don't care.
  745. */
  746. (void)local_cmpxchg(&next_page->write, old_write, val);
  747. (void)local_cmpxchg(&next_page->entries, old_entries, eval);
  748. /*
  749. * No need to worry about races with clearing out the commit.
  750. * it only can increment when a commit takes place. But that
  751. * only happens in the outer most nested commit.
  752. */
  753. local_set(&next_page->page->commit, 0);
  754. old_tail = cmpxchg(&cpu_buffer->tail_page,
  755. tail_page, next_page);
  756. if (old_tail == tail_page)
  757. ret = 1;
  758. }
  759. return ret;
  760. }
  761. static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
  762. struct buffer_page *bpage)
  763. {
  764. unsigned long val = (unsigned long)bpage;
  765. if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
  766. return 1;
  767. return 0;
  768. }
  769. /**
  770. * rb_check_list - make sure a pointer to a list has the last bits zero
  771. */
  772. static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
  773. struct list_head *list)
  774. {
  775. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
  776. return 1;
  777. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
  778. return 1;
  779. return 0;
  780. }
  781. /**
  782. * check_pages - integrity check of buffer pages
  783. * @cpu_buffer: CPU buffer with pages to test
  784. *
  785. * As a safety measure we check to make sure the data pages have not
  786. * been corrupted.
  787. */
  788. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  789. {
  790. struct list_head *head = cpu_buffer->pages;
  791. struct buffer_page *bpage, *tmp;
  792. rb_head_page_deactivate(cpu_buffer);
  793. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  794. return -1;
  795. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  796. return -1;
  797. if (rb_check_list(cpu_buffer, head))
  798. return -1;
  799. list_for_each_entry_safe(bpage, tmp, head, list) {
  800. if (RB_WARN_ON(cpu_buffer,
  801. bpage->list.next->prev != &bpage->list))
  802. return -1;
  803. if (RB_WARN_ON(cpu_buffer,
  804. bpage->list.prev->next != &bpage->list))
  805. return -1;
  806. if (rb_check_list(cpu_buffer, &bpage->list))
  807. return -1;
  808. }
  809. rb_head_page_activate(cpu_buffer);
  810. return 0;
  811. }
  812. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  813. unsigned nr_pages)
  814. {
  815. struct buffer_page *bpage, *tmp;
  816. unsigned long addr;
  817. LIST_HEAD(pages);
  818. unsigned i;
  819. WARN_ON(!nr_pages);
  820. for (i = 0; i < nr_pages; i++) {
  821. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  822. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  823. if (!bpage)
  824. goto free_pages;
  825. rb_check_bpage(cpu_buffer, bpage);
  826. list_add(&bpage->list, &pages);
  827. addr = __get_free_page(GFP_KERNEL);
  828. if (!addr)
  829. goto free_pages;
  830. bpage->page = (void *)addr;
  831. rb_init_page(bpage->page);
  832. }
  833. /*
  834. * The ring buffer page list is a circular list that does not
  835. * start and end with a list head. All page list items point to
  836. * other pages.
  837. */
  838. cpu_buffer->pages = pages.next;
  839. list_del(&pages);
  840. rb_check_pages(cpu_buffer);
  841. return 0;
  842. free_pages:
  843. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  844. list_del_init(&bpage->list);
  845. free_buffer_page(bpage);
  846. }
  847. return -ENOMEM;
  848. }
  849. static struct ring_buffer_per_cpu *
  850. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  851. {
  852. struct ring_buffer_per_cpu *cpu_buffer;
  853. struct buffer_page *bpage;
  854. unsigned long addr;
  855. int ret;
  856. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  857. GFP_KERNEL, cpu_to_node(cpu));
  858. if (!cpu_buffer)
  859. return NULL;
  860. cpu_buffer->cpu = cpu;
  861. cpu_buffer->buffer = buffer;
  862. spin_lock_init(&cpu_buffer->reader_lock);
  863. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  864. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  865. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  866. GFP_KERNEL, cpu_to_node(cpu));
  867. if (!bpage)
  868. goto fail_free_buffer;
  869. rb_check_bpage(cpu_buffer, bpage);
  870. cpu_buffer->reader_page = bpage;
  871. addr = __get_free_page(GFP_KERNEL);
  872. if (!addr)
  873. goto fail_free_reader;
  874. bpage->page = (void *)addr;
  875. rb_init_page(bpage->page);
  876. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  877. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  878. if (ret < 0)
  879. goto fail_free_reader;
  880. cpu_buffer->head_page
  881. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  882. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  883. rb_head_page_activate(cpu_buffer);
  884. return cpu_buffer;
  885. fail_free_reader:
  886. free_buffer_page(cpu_buffer->reader_page);
  887. fail_free_buffer:
  888. kfree(cpu_buffer);
  889. return NULL;
  890. }
  891. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  892. {
  893. struct list_head *head = cpu_buffer->pages;
  894. struct buffer_page *bpage, *tmp;
  895. free_buffer_page(cpu_buffer->reader_page);
  896. rb_head_page_deactivate(cpu_buffer);
  897. if (head) {
  898. list_for_each_entry_safe(bpage, tmp, head, list) {
  899. list_del_init(&bpage->list);
  900. free_buffer_page(bpage);
  901. }
  902. bpage = list_entry(head, struct buffer_page, list);
  903. free_buffer_page(bpage);
  904. }
  905. kfree(cpu_buffer);
  906. }
  907. #ifdef CONFIG_HOTPLUG_CPU
  908. static int rb_cpu_notify(struct notifier_block *self,
  909. unsigned long action, void *hcpu);
  910. #endif
  911. /**
  912. * ring_buffer_alloc - allocate a new ring_buffer
  913. * @size: the size in bytes per cpu that is needed.
  914. * @flags: attributes to set for the ring buffer.
  915. *
  916. * Currently the only flag that is available is the RB_FL_OVERWRITE
  917. * flag. This flag means that the buffer will overwrite old data
  918. * when the buffer wraps. If this flag is not set, the buffer will
  919. * drop data when the tail hits the head.
  920. */
  921. struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  922. struct lock_class_key *key)
  923. {
  924. struct ring_buffer *buffer;
  925. int bsize;
  926. int cpu;
  927. /* keep it in its own cache line */
  928. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  929. GFP_KERNEL);
  930. if (!buffer)
  931. return NULL;
  932. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  933. goto fail_free_buffer;
  934. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  935. buffer->flags = flags;
  936. buffer->clock = trace_clock_local;
  937. buffer->reader_lock_key = key;
  938. /* need at least two pages */
  939. if (buffer->pages < 2)
  940. buffer->pages = 2;
  941. /*
  942. * In case of non-hotplug cpu, if the ring-buffer is allocated
  943. * in early initcall, it will not be notified of secondary cpus.
  944. * In that off case, we need to allocate for all possible cpus.
  945. */
  946. #ifdef CONFIG_HOTPLUG_CPU
  947. get_online_cpus();
  948. cpumask_copy(buffer->cpumask, cpu_online_mask);
  949. #else
  950. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  951. #endif
  952. buffer->cpus = nr_cpu_ids;
  953. bsize = sizeof(void *) * nr_cpu_ids;
  954. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  955. GFP_KERNEL);
  956. if (!buffer->buffers)
  957. goto fail_free_cpumask;
  958. for_each_buffer_cpu(buffer, cpu) {
  959. buffer->buffers[cpu] =
  960. rb_allocate_cpu_buffer(buffer, cpu);
  961. if (!buffer->buffers[cpu])
  962. goto fail_free_buffers;
  963. }
  964. #ifdef CONFIG_HOTPLUG_CPU
  965. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  966. buffer->cpu_notify.priority = 0;
  967. register_cpu_notifier(&buffer->cpu_notify);
  968. #endif
  969. put_online_cpus();
  970. mutex_init(&buffer->mutex);
  971. return buffer;
  972. fail_free_buffers:
  973. for_each_buffer_cpu(buffer, cpu) {
  974. if (buffer->buffers[cpu])
  975. rb_free_cpu_buffer(buffer->buffers[cpu]);
  976. }
  977. kfree(buffer->buffers);
  978. fail_free_cpumask:
  979. free_cpumask_var(buffer->cpumask);
  980. put_online_cpus();
  981. fail_free_buffer:
  982. kfree(buffer);
  983. return NULL;
  984. }
  985. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  986. /**
  987. * ring_buffer_free - free a ring buffer.
  988. * @buffer: the buffer to free.
  989. */
  990. void
  991. ring_buffer_free(struct ring_buffer *buffer)
  992. {
  993. int cpu;
  994. get_online_cpus();
  995. #ifdef CONFIG_HOTPLUG_CPU
  996. unregister_cpu_notifier(&buffer->cpu_notify);
  997. #endif
  998. for_each_buffer_cpu(buffer, cpu)
  999. rb_free_cpu_buffer(buffer->buffers[cpu]);
  1000. put_online_cpus();
  1001. kfree(buffer->buffers);
  1002. free_cpumask_var(buffer->cpumask);
  1003. kfree(buffer);
  1004. }
  1005. EXPORT_SYMBOL_GPL(ring_buffer_free);
  1006. void ring_buffer_set_clock(struct ring_buffer *buffer,
  1007. u64 (*clock)(void))
  1008. {
  1009. buffer->clock = clock;
  1010. }
  1011. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  1012. static void
  1013. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  1014. {
  1015. struct buffer_page *bpage;
  1016. struct list_head *p;
  1017. unsigned i;
  1018. atomic_inc(&cpu_buffer->record_disabled);
  1019. synchronize_sched();
  1020. spin_lock_irq(&cpu_buffer->reader_lock);
  1021. rb_head_page_deactivate(cpu_buffer);
  1022. for (i = 0; i < nr_pages; i++) {
  1023. if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
  1024. return;
  1025. p = cpu_buffer->pages->next;
  1026. bpage = list_entry(p, struct buffer_page, list);
  1027. list_del_init(&bpage->list);
  1028. free_buffer_page(bpage);
  1029. }
  1030. if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
  1031. return;
  1032. rb_reset_cpu(cpu_buffer);
  1033. spin_unlock_irq(&cpu_buffer->reader_lock);
  1034. rb_check_pages(cpu_buffer);
  1035. atomic_dec(&cpu_buffer->record_disabled);
  1036. }
  1037. static void
  1038. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1039. struct list_head *pages, unsigned nr_pages)
  1040. {
  1041. struct buffer_page *bpage;
  1042. struct list_head *p;
  1043. unsigned i;
  1044. atomic_inc(&cpu_buffer->record_disabled);
  1045. synchronize_sched();
  1046. spin_lock_irq(&cpu_buffer->reader_lock);
  1047. rb_head_page_deactivate(cpu_buffer);
  1048. for (i = 0; i < nr_pages; i++) {
  1049. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  1050. return;
  1051. p = pages->next;
  1052. bpage = list_entry(p, struct buffer_page, list);
  1053. list_del_init(&bpage->list);
  1054. list_add_tail(&bpage->list, cpu_buffer->pages);
  1055. }
  1056. rb_reset_cpu(cpu_buffer);
  1057. spin_unlock_irq(&cpu_buffer->reader_lock);
  1058. rb_check_pages(cpu_buffer);
  1059. atomic_dec(&cpu_buffer->record_disabled);
  1060. }
  1061. /**
  1062. * ring_buffer_resize - resize the ring buffer
  1063. * @buffer: the buffer to resize.
  1064. * @size: the new size.
  1065. *
  1066. * The tracer is responsible for making sure that the buffer is
  1067. * not being used while changing the size.
  1068. * Note: We may be able to change the above requirement by using
  1069. * RCU synchronizations.
  1070. *
  1071. * Minimum size is 2 * BUF_PAGE_SIZE.
  1072. *
  1073. * Returns -1 on failure.
  1074. */
  1075. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  1076. {
  1077. struct ring_buffer_per_cpu *cpu_buffer;
  1078. unsigned nr_pages, rm_pages, new_pages;
  1079. struct buffer_page *bpage, *tmp;
  1080. unsigned long buffer_size;
  1081. unsigned long addr;
  1082. LIST_HEAD(pages);
  1083. int i, cpu;
  1084. /*
  1085. * Always succeed at resizing a non-existent buffer:
  1086. */
  1087. if (!buffer)
  1088. return size;
  1089. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1090. size *= BUF_PAGE_SIZE;
  1091. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  1092. /* we need a minimum of two pages */
  1093. if (size < BUF_PAGE_SIZE * 2)
  1094. size = BUF_PAGE_SIZE * 2;
  1095. if (size == buffer_size)
  1096. return size;
  1097. mutex_lock(&buffer->mutex);
  1098. get_online_cpus();
  1099. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1100. if (size < buffer_size) {
  1101. /* easy case, just free pages */
  1102. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  1103. goto out_fail;
  1104. rm_pages = buffer->pages - nr_pages;
  1105. for_each_buffer_cpu(buffer, cpu) {
  1106. cpu_buffer = buffer->buffers[cpu];
  1107. rb_remove_pages(cpu_buffer, rm_pages);
  1108. }
  1109. goto out;
  1110. }
  1111. /*
  1112. * This is a bit more difficult. We only want to add pages
  1113. * when we can allocate enough for all CPUs. We do this
  1114. * by allocating all the pages and storing them on a local
  1115. * link list. If we succeed in our allocation, then we
  1116. * add these pages to the cpu_buffers. Otherwise we just free
  1117. * them all and return -ENOMEM;
  1118. */
  1119. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  1120. goto out_fail;
  1121. new_pages = nr_pages - buffer->pages;
  1122. for_each_buffer_cpu(buffer, cpu) {
  1123. for (i = 0; i < new_pages; i++) {
  1124. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  1125. cache_line_size()),
  1126. GFP_KERNEL, cpu_to_node(cpu));
  1127. if (!bpage)
  1128. goto free_pages;
  1129. list_add(&bpage->list, &pages);
  1130. addr = __get_free_page(GFP_KERNEL);
  1131. if (!addr)
  1132. goto free_pages;
  1133. bpage->page = (void *)addr;
  1134. rb_init_page(bpage->page);
  1135. }
  1136. }
  1137. for_each_buffer_cpu(buffer, cpu) {
  1138. cpu_buffer = buffer->buffers[cpu];
  1139. rb_insert_pages(cpu_buffer, &pages, new_pages);
  1140. }
  1141. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  1142. goto out_fail;
  1143. out:
  1144. buffer->pages = nr_pages;
  1145. put_online_cpus();
  1146. mutex_unlock(&buffer->mutex);
  1147. return size;
  1148. free_pages:
  1149. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  1150. list_del_init(&bpage->list);
  1151. free_buffer_page(bpage);
  1152. }
  1153. put_online_cpus();
  1154. mutex_unlock(&buffer->mutex);
  1155. return -ENOMEM;
  1156. /*
  1157. * Something went totally wrong, and we are too paranoid
  1158. * to even clean up the mess.
  1159. */
  1160. out_fail:
  1161. put_online_cpus();
  1162. mutex_unlock(&buffer->mutex);
  1163. return -1;
  1164. }
  1165. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  1166. static inline void *
  1167. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  1168. {
  1169. return bpage->data + index;
  1170. }
  1171. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  1172. {
  1173. return bpage->page->data + index;
  1174. }
  1175. static inline struct ring_buffer_event *
  1176. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  1177. {
  1178. return __rb_page_index(cpu_buffer->reader_page,
  1179. cpu_buffer->reader_page->read);
  1180. }
  1181. static inline struct ring_buffer_event *
  1182. rb_iter_head_event(struct ring_buffer_iter *iter)
  1183. {
  1184. return __rb_page_index(iter->head_page, iter->head);
  1185. }
  1186. static inline unsigned long rb_page_write(struct buffer_page *bpage)
  1187. {
  1188. return local_read(&bpage->write) & RB_WRITE_MASK;
  1189. }
  1190. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  1191. {
  1192. return local_read(&bpage->page->commit);
  1193. }
  1194. static inline unsigned long rb_page_entries(struct buffer_page *bpage)
  1195. {
  1196. return local_read(&bpage->entries) & RB_WRITE_MASK;
  1197. }
  1198. /* Size is determined by what has been commited */
  1199. static inline unsigned rb_page_size(struct buffer_page *bpage)
  1200. {
  1201. return rb_page_commit(bpage);
  1202. }
  1203. static inline unsigned
  1204. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  1205. {
  1206. return rb_page_commit(cpu_buffer->commit_page);
  1207. }
  1208. static inline unsigned
  1209. rb_event_index(struct ring_buffer_event *event)
  1210. {
  1211. unsigned long addr = (unsigned long)event;
  1212. return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
  1213. }
  1214. static inline int
  1215. rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1216. struct ring_buffer_event *event)
  1217. {
  1218. unsigned long addr = (unsigned long)event;
  1219. unsigned long index;
  1220. index = rb_event_index(event);
  1221. addr &= PAGE_MASK;
  1222. return cpu_buffer->commit_page->page == (void *)addr &&
  1223. rb_commit_index(cpu_buffer) == index;
  1224. }
  1225. static void
  1226. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  1227. {
  1228. unsigned long max_count;
  1229. /*
  1230. * We only race with interrupts and NMIs on this CPU.
  1231. * If we own the commit event, then we can commit
  1232. * all others that interrupted us, since the interruptions
  1233. * are in stack format (they finish before they come
  1234. * back to us). This allows us to do a simple loop to
  1235. * assign the commit to the tail.
  1236. */
  1237. again:
  1238. max_count = cpu_buffer->buffer->pages * 100;
  1239. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  1240. if (RB_WARN_ON(cpu_buffer, !(--max_count)))
  1241. return;
  1242. if (RB_WARN_ON(cpu_buffer,
  1243. rb_is_reader_page(cpu_buffer->tail_page)))
  1244. return;
  1245. local_set(&cpu_buffer->commit_page->page->commit,
  1246. rb_page_write(cpu_buffer->commit_page));
  1247. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  1248. cpu_buffer->write_stamp =
  1249. cpu_buffer->commit_page->page->time_stamp;
  1250. /* add barrier to keep gcc from optimizing too much */
  1251. barrier();
  1252. }
  1253. while (rb_commit_index(cpu_buffer) !=
  1254. rb_page_write(cpu_buffer->commit_page)) {
  1255. local_set(&cpu_buffer->commit_page->page->commit,
  1256. rb_page_write(cpu_buffer->commit_page));
  1257. RB_WARN_ON(cpu_buffer,
  1258. local_read(&cpu_buffer->commit_page->page->commit) &
  1259. ~RB_WRITE_MASK);
  1260. barrier();
  1261. }
  1262. /* again, keep gcc from optimizing */
  1263. barrier();
  1264. /*
  1265. * If an interrupt came in just after the first while loop
  1266. * and pushed the tail page forward, we will be left with
  1267. * a dangling commit that will never go forward.
  1268. */
  1269. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  1270. goto again;
  1271. }
  1272. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1273. {
  1274. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  1275. cpu_buffer->reader_page->read = 0;
  1276. }
  1277. static void rb_inc_iter(struct ring_buffer_iter *iter)
  1278. {
  1279. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1280. /*
  1281. * The iterator could be on the reader page (it starts there).
  1282. * But the head could have moved, since the reader was
  1283. * found. Check for this case and assign the iterator
  1284. * to the head page instead of next.
  1285. */
  1286. if (iter->head_page == cpu_buffer->reader_page)
  1287. iter->head_page = rb_set_head_page(cpu_buffer);
  1288. else
  1289. rb_inc_page(cpu_buffer, &iter->head_page);
  1290. iter->read_stamp = iter->head_page->page->time_stamp;
  1291. iter->head = 0;
  1292. }
  1293. /**
  1294. * ring_buffer_update_event - update event type and data
  1295. * @event: the even to update
  1296. * @type: the type of event
  1297. * @length: the size of the event field in the ring buffer
  1298. *
  1299. * Update the type and data fields of the event. The length
  1300. * is the actual size that is written to the ring buffer,
  1301. * and with this, we can determine what to place into the
  1302. * data field.
  1303. */
  1304. static void
  1305. rb_update_event(struct ring_buffer_event *event,
  1306. unsigned type, unsigned length)
  1307. {
  1308. event->type_len = type;
  1309. switch (type) {
  1310. case RINGBUF_TYPE_PADDING:
  1311. case RINGBUF_TYPE_TIME_EXTEND:
  1312. case RINGBUF_TYPE_TIME_STAMP:
  1313. break;
  1314. case 0:
  1315. length -= RB_EVNT_HDR_SIZE;
  1316. if (length > RB_MAX_SMALL_DATA)
  1317. event->array[0] = length;
  1318. else
  1319. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  1320. break;
  1321. default:
  1322. BUG();
  1323. }
  1324. }
  1325. /*
  1326. * rb_handle_head_page - writer hit the head page
  1327. *
  1328. * Returns: +1 to retry page
  1329. * 0 to continue
  1330. * -1 on error
  1331. */
  1332. static int
  1333. rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  1334. struct buffer_page *tail_page,
  1335. struct buffer_page *next_page)
  1336. {
  1337. struct buffer_page *new_head;
  1338. int entries;
  1339. int type;
  1340. int ret;
  1341. entries = rb_page_entries(next_page);
  1342. /*
  1343. * The hard part is here. We need to move the head
  1344. * forward, and protect against both readers on
  1345. * other CPUs and writers coming in via interrupts.
  1346. */
  1347. type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
  1348. RB_PAGE_HEAD);
  1349. /*
  1350. * type can be one of four:
  1351. * NORMAL - an interrupt already moved it for us
  1352. * HEAD - we are the first to get here.
  1353. * UPDATE - we are the interrupt interrupting
  1354. * a current move.
  1355. * MOVED - a reader on another CPU moved the next
  1356. * pointer to its reader page. Give up
  1357. * and try again.
  1358. */
  1359. switch (type) {
  1360. case RB_PAGE_HEAD:
  1361. /*
  1362. * We changed the head to UPDATE, thus
  1363. * it is our responsibility to update
  1364. * the counters.
  1365. */
  1366. local_add(entries, &cpu_buffer->overrun);
  1367. /*
  1368. * The entries will be zeroed out when we move the
  1369. * tail page.
  1370. */
  1371. /* still more to do */
  1372. break;
  1373. case RB_PAGE_UPDATE:
  1374. /*
  1375. * This is an interrupt that interrupt the
  1376. * previous update. Still more to do.
  1377. */
  1378. break;
  1379. case RB_PAGE_NORMAL:
  1380. /*
  1381. * An interrupt came in before the update
  1382. * and processed this for us.
  1383. * Nothing left to do.
  1384. */
  1385. return 1;
  1386. case RB_PAGE_MOVED:
  1387. /*
  1388. * The reader is on another CPU and just did
  1389. * a swap with our next_page.
  1390. * Try again.
  1391. */
  1392. return 1;
  1393. default:
  1394. RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
  1395. return -1;
  1396. }
  1397. /*
  1398. * Now that we are here, the old head pointer is
  1399. * set to UPDATE. This will keep the reader from
  1400. * swapping the head page with the reader page.
  1401. * The reader (on another CPU) will spin till
  1402. * we are finished.
  1403. *
  1404. * We just need to protect against interrupts
  1405. * doing the job. We will set the next pointer
  1406. * to HEAD. After that, we set the old pointer
  1407. * to NORMAL, but only if it was HEAD before.
  1408. * otherwise we are an interrupt, and only
  1409. * want the outer most commit to reset it.
  1410. */
  1411. new_head = next_page;
  1412. rb_inc_page(cpu_buffer, &new_head);
  1413. ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
  1414. RB_PAGE_NORMAL);
  1415. /*
  1416. * Valid returns are:
  1417. * HEAD - an interrupt came in and already set it.
  1418. * NORMAL - One of two things:
  1419. * 1) We really set it.
  1420. * 2) A bunch of interrupts came in and moved
  1421. * the page forward again.
  1422. */
  1423. switch (ret) {
  1424. case RB_PAGE_HEAD:
  1425. case RB_PAGE_NORMAL:
  1426. /* OK */
  1427. break;
  1428. default:
  1429. RB_WARN_ON(cpu_buffer, 1);
  1430. return -1;
  1431. }
  1432. /*
  1433. * It is possible that an interrupt came in,
  1434. * set the head up, then more interrupts came in
  1435. * and moved it again. When we get back here,
  1436. * the page would have been set to NORMAL but we
  1437. * just set it back to HEAD.
  1438. *
  1439. * How do you detect this? Well, if that happened
  1440. * the tail page would have moved.
  1441. */
  1442. if (ret == RB_PAGE_NORMAL) {
  1443. /*
  1444. * If the tail had moved passed next, then we need
  1445. * to reset the pointer.
  1446. */
  1447. if (cpu_buffer->tail_page != tail_page &&
  1448. cpu_buffer->tail_page != next_page)
  1449. rb_head_page_set_normal(cpu_buffer, new_head,
  1450. next_page,
  1451. RB_PAGE_HEAD);
  1452. }
  1453. /*
  1454. * If this was the outer most commit (the one that
  1455. * changed the original pointer from HEAD to UPDATE),
  1456. * then it is up to us to reset it to NORMAL.
  1457. */
  1458. if (type == RB_PAGE_HEAD) {
  1459. ret = rb_head_page_set_normal(cpu_buffer, next_page,
  1460. tail_page,
  1461. RB_PAGE_UPDATE);
  1462. if (RB_WARN_ON(cpu_buffer,
  1463. ret != RB_PAGE_UPDATE))
  1464. return -1;
  1465. }
  1466. return 0;
  1467. }
  1468. static unsigned rb_calculate_event_length(unsigned length)
  1469. {
  1470. struct ring_buffer_event event; /* Used only for sizeof array */
  1471. /* zero length can cause confusions */
  1472. if (!length)
  1473. length = 1;
  1474. if (length > RB_MAX_SMALL_DATA)
  1475. length += sizeof(event.array[0]);
  1476. length += RB_EVNT_HDR_SIZE;
  1477. length = ALIGN(length, RB_ALIGNMENT);
  1478. return length;
  1479. }
  1480. static inline void
  1481. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1482. struct buffer_page *tail_page,
  1483. unsigned long tail, unsigned long length)
  1484. {
  1485. struct ring_buffer_event *event;
  1486. /*
  1487. * Only the event that crossed the page boundary
  1488. * must fill the old tail_page with padding.
  1489. */
  1490. if (tail >= BUF_PAGE_SIZE) {
  1491. local_sub(length, &tail_page->write);
  1492. return;
  1493. }
  1494. event = __rb_page_index(tail_page, tail);
  1495. kmemcheck_annotate_bitfield(event, bitfield);
  1496. /*
  1497. * If this event is bigger than the minimum size, then
  1498. * we need to be careful that we don't subtract the
  1499. * write counter enough to allow another writer to slip
  1500. * in on this page.
  1501. * We put in a discarded commit instead, to make sure
  1502. * that this space is not used again.
  1503. *
  1504. * If we are less than the minimum size, we don't need to
  1505. * worry about it.
  1506. */
  1507. if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
  1508. /* No room for any events */
  1509. /* Mark the rest of the page with padding */
  1510. rb_event_set_padding(event);
  1511. /* Set the write back to the previous setting */
  1512. local_sub(length, &tail_page->write);
  1513. return;
  1514. }
  1515. /* Put in a discarded event */
  1516. event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
  1517. event->type_len = RINGBUF_TYPE_PADDING;
  1518. /* time delta must be non zero */
  1519. event->time_delta = 1;
  1520. /* Set write to end of buffer */
  1521. length = (tail + length) - BUF_PAGE_SIZE;
  1522. local_sub(length, &tail_page->write);
  1523. }
  1524. static struct ring_buffer_event *
  1525. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1526. unsigned long length, unsigned long tail,
  1527. struct buffer_page *commit_page,
  1528. struct buffer_page *tail_page, u64 *ts)
  1529. {
  1530. struct ring_buffer *buffer = cpu_buffer->buffer;
  1531. struct buffer_page *next_page;
  1532. int ret;
  1533. next_page = tail_page;
  1534. rb_inc_page(cpu_buffer, &next_page);
  1535. /*
  1536. * If for some reason, we had an interrupt storm that made
  1537. * it all the way around the buffer, bail, and warn
  1538. * about it.
  1539. */
  1540. if (unlikely(next_page == commit_page)) {
  1541. local_inc(&cpu_buffer->commit_overrun);
  1542. goto out_reset;
  1543. }
  1544. /*
  1545. * This is where the fun begins!
  1546. *
  1547. * We are fighting against races between a reader that
  1548. * could be on another CPU trying to swap its reader
  1549. * page with the buffer head.
  1550. *
  1551. * We are also fighting against interrupts coming in and
  1552. * moving the head or tail on us as well.
  1553. *
  1554. * If the next page is the head page then we have filled
  1555. * the buffer, unless the commit page is still on the
  1556. * reader page.
  1557. */
  1558. if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
  1559. /*
  1560. * If the commit is not on the reader page, then
  1561. * move the header page.
  1562. */
  1563. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  1564. /*
  1565. * If we are not in overwrite mode,
  1566. * this is easy, just stop here.
  1567. */
  1568. if (!(buffer->flags & RB_FL_OVERWRITE))
  1569. goto out_reset;
  1570. ret = rb_handle_head_page(cpu_buffer,
  1571. tail_page,
  1572. next_page);
  1573. if (ret < 0)
  1574. goto out_reset;
  1575. if (ret)
  1576. goto out_again;
  1577. } else {
  1578. /*
  1579. * We need to be careful here too. The
  1580. * commit page could still be on the reader
  1581. * page. We could have a small buffer, and
  1582. * have filled up the buffer with events
  1583. * from interrupts and such, and wrapped.
  1584. *
  1585. * Note, if the tail page is also the on the
  1586. * reader_page, we let it move out.
  1587. */
  1588. if (unlikely((cpu_buffer->commit_page !=
  1589. cpu_buffer->tail_page) &&
  1590. (cpu_buffer->commit_page ==
  1591. cpu_buffer->reader_page))) {
  1592. local_inc(&cpu_buffer->commit_overrun);
  1593. goto out_reset;
  1594. }
  1595. }
  1596. }
  1597. ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
  1598. if (ret) {
  1599. /*
  1600. * Nested commits always have zero deltas, so
  1601. * just reread the time stamp
  1602. */
  1603. *ts = rb_time_stamp(buffer);
  1604. next_page->page->time_stamp = *ts;
  1605. }
  1606. out_again:
  1607. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1608. /* fail and let the caller try again */
  1609. return ERR_PTR(-EAGAIN);
  1610. out_reset:
  1611. /* reset write */
  1612. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1613. return NULL;
  1614. }
  1615. static struct ring_buffer_event *
  1616. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  1617. unsigned type, unsigned long length, u64 *ts)
  1618. {
  1619. struct buffer_page *tail_page, *commit_page;
  1620. struct ring_buffer_event *event;
  1621. unsigned long tail, write;
  1622. commit_page = cpu_buffer->commit_page;
  1623. /* we just need to protect against interrupts */
  1624. barrier();
  1625. tail_page = cpu_buffer->tail_page;
  1626. write = local_add_return(length, &tail_page->write);
  1627. /* set write to only the index of the write */
  1628. write &= RB_WRITE_MASK;
  1629. tail = write - length;
  1630. /* See if we shot pass the end of this buffer page */
  1631. if (write > BUF_PAGE_SIZE)
  1632. return rb_move_tail(cpu_buffer, length, tail,
  1633. commit_page, tail_page, ts);
  1634. /* We reserved something on the buffer */
  1635. event = __rb_page_index(tail_page, tail);
  1636. kmemcheck_annotate_bitfield(event, bitfield);
  1637. rb_update_event(event, type, length);
  1638. /* The passed in type is zero for DATA */
  1639. if (likely(!type))
  1640. local_inc(&tail_page->entries);
  1641. /*
  1642. * If this is the first commit on the page, then update
  1643. * its timestamp.
  1644. */
  1645. if (!tail)
  1646. tail_page->page->time_stamp = *ts;
  1647. return event;
  1648. }
  1649. static inline int
  1650. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  1651. struct ring_buffer_event *event)
  1652. {
  1653. unsigned long new_index, old_index;
  1654. struct buffer_page *bpage;
  1655. unsigned long index;
  1656. unsigned long addr;
  1657. new_index = rb_event_index(event);
  1658. old_index = new_index + rb_event_length(event);
  1659. addr = (unsigned long)event;
  1660. addr &= PAGE_MASK;
  1661. bpage = cpu_buffer->tail_page;
  1662. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  1663. unsigned long write_mask =
  1664. local_read(&bpage->write) & ~RB_WRITE_MASK;
  1665. /*
  1666. * This is on the tail page. It is possible that
  1667. * a write could come in and move the tail page
  1668. * and write to the next page. That is fine
  1669. * because we just shorten what is on this page.
  1670. */
  1671. old_index += write_mask;
  1672. new_index += write_mask;
  1673. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1674. if (index == old_index)
  1675. return 1;
  1676. }
  1677. /* could not discard */
  1678. return 0;
  1679. }
  1680. static int
  1681. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1682. u64 *ts, u64 *delta)
  1683. {
  1684. struct ring_buffer_event *event;
  1685. static int once;
  1686. int ret;
  1687. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1688. printk(KERN_WARNING "Delta way too big! %llu"
  1689. " ts=%llu write stamp = %llu\n",
  1690. (unsigned long long)*delta,
  1691. (unsigned long long)*ts,
  1692. (unsigned long long)cpu_buffer->write_stamp);
  1693. WARN_ON(1);
  1694. }
  1695. /*
  1696. * The delta is too big, we to add a
  1697. * new timestamp.
  1698. */
  1699. event = __rb_reserve_next(cpu_buffer,
  1700. RINGBUF_TYPE_TIME_EXTEND,
  1701. RB_LEN_TIME_EXTEND,
  1702. ts);
  1703. if (!event)
  1704. return -EBUSY;
  1705. if (PTR_ERR(event) == -EAGAIN)
  1706. return -EAGAIN;
  1707. /* Only a commited time event can update the write stamp */
  1708. if (rb_event_is_commit(cpu_buffer, event)) {
  1709. /*
  1710. * If this is the first on the page, then it was
  1711. * updated with the page itself. Try to discard it
  1712. * and if we can't just make it zero.
  1713. */
  1714. if (rb_event_index(event)) {
  1715. event->time_delta = *delta & TS_MASK;
  1716. event->array[0] = *delta >> TS_SHIFT;
  1717. } else {
  1718. /* try to discard, since we do not need this */
  1719. if (!rb_try_to_discard(cpu_buffer, event)) {
  1720. /* nope, just zero it */
  1721. event->time_delta = 0;
  1722. event->array[0] = 0;
  1723. }
  1724. }
  1725. cpu_buffer->write_stamp = *ts;
  1726. /* let the caller know this was the commit */
  1727. ret = 1;
  1728. } else {
  1729. /* Try to discard the event */
  1730. if (!rb_try_to_discard(cpu_buffer, event)) {
  1731. /* Darn, this is just wasted space */
  1732. event->time_delta = 0;
  1733. event->array[0] = 0;
  1734. }
  1735. ret = 0;
  1736. }
  1737. *delta = 0;
  1738. return ret;
  1739. }
  1740. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1741. {
  1742. local_inc(&cpu_buffer->committing);
  1743. local_inc(&cpu_buffer->commits);
  1744. }
  1745. static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1746. {
  1747. unsigned long commits;
  1748. if (RB_WARN_ON(cpu_buffer,
  1749. !local_read(&cpu_buffer->committing)))
  1750. return;
  1751. again:
  1752. commits = local_read(&cpu_buffer->commits);
  1753. /* synchronize with interrupts */
  1754. barrier();
  1755. if (local_read(&cpu_buffer->committing) == 1)
  1756. rb_set_commit_to_write(cpu_buffer);
  1757. local_dec(&cpu_buffer->committing);
  1758. /* synchronize with interrupts */
  1759. barrier();
  1760. /*
  1761. * Need to account for interrupts coming in between the
  1762. * updating of the commit page and the clearing of the
  1763. * committing counter.
  1764. */
  1765. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  1766. !local_read(&cpu_buffer->committing)) {
  1767. local_inc(&cpu_buffer->committing);
  1768. goto again;
  1769. }
  1770. }
  1771. static struct ring_buffer_event *
  1772. rb_reserve_next_event(struct ring_buffer *buffer,
  1773. struct ring_buffer_per_cpu *cpu_buffer,
  1774. unsigned long length)
  1775. {
  1776. struct ring_buffer_event *event;
  1777. u64 ts, delta = 0;
  1778. int commit = 0;
  1779. int nr_loops = 0;
  1780. rb_start_commit(cpu_buffer);
  1781. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  1782. /*
  1783. * Due to the ability to swap a cpu buffer from a buffer
  1784. * it is possible it was swapped before we committed.
  1785. * (committing stops a swap). We check for it here and
  1786. * if it happened, we have to fail the write.
  1787. */
  1788. barrier();
  1789. if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
  1790. local_dec(&cpu_buffer->committing);
  1791. local_dec(&cpu_buffer->commits);
  1792. return NULL;
  1793. }
  1794. #endif
  1795. length = rb_calculate_event_length(length);
  1796. again:
  1797. /*
  1798. * We allow for interrupts to reenter here and do a trace.
  1799. * If one does, it will cause this original code to loop
  1800. * back here. Even with heavy interrupts happening, this
  1801. * should only happen a few times in a row. If this happens
  1802. * 1000 times in a row, there must be either an interrupt
  1803. * storm or we have something buggy.
  1804. * Bail!
  1805. */
  1806. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1807. goto out_fail;
  1808. ts = rb_time_stamp(cpu_buffer->buffer);
  1809. /*
  1810. * Only the first commit can update the timestamp.
  1811. * Yes there is a race here. If an interrupt comes in
  1812. * just after the conditional and it traces too, then it
  1813. * will also check the deltas. More than one timestamp may
  1814. * also be made. But only the entry that did the actual
  1815. * commit will be something other than zero.
  1816. */
  1817. if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1818. rb_page_write(cpu_buffer->tail_page) ==
  1819. rb_commit_index(cpu_buffer))) {
  1820. u64 diff;
  1821. diff = ts - cpu_buffer->write_stamp;
  1822. /* make sure this diff is calculated here */
  1823. barrier();
  1824. /* Did the write stamp get updated already? */
  1825. if (unlikely(ts < cpu_buffer->write_stamp))
  1826. goto get_event;
  1827. delta = diff;
  1828. if (unlikely(test_time_stamp(delta))) {
  1829. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1830. if (commit == -EBUSY)
  1831. goto out_fail;
  1832. if (commit == -EAGAIN)
  1833. goto again;
  1834. RB_WARN_ON(cpu_buffer, commit < 0);
  1835. }
  1836. }
  1837. get_event:
  1838. event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
  1839. if (unlikely(PTR_ERR(event) == -EAGAIN))
  1840. goto again;
  1841. if (!event)
  1842. goto out_fail;
  1843. if (!rb_event_is_commit(cpu_buffer, event))
  1844. delta = 0;
  1845. event->time_delta = delta;
  1846. return event;
  1847. out_fail:
  1848. rb_end_commit(cpu_buffer);
  1849. return NULL;
  1850. }
  1851. #ifdef CONFIG_TRACING
  1852. #define TRACE_RECURSIVE_DEPTH 16
  1853. static int trace_recursive_lock(void)
  1854. {
  1855. current->trace_recursion++;
  1856. if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
  1857. return 0;
  1858. /* Disable all tracing before we do anything else */
  1859. tracing_off_permanent();
  1860. printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
  1861. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1862. current->trace_recursion,
  1863. hardirq_count() >> HARDIRQ_SHIFT,
  1864. softirq_count() >> SOFTIRQ_SHIFT,
  1865. in_nmi());
  1866. WARN_ON_ONCE(1);
  1867. return -1;
  1868. }
  1869. static void trace_recursive_unlock(void)
  1870. {
  1871. WARN_ON_ONCE(!current->trace_recursion);
  1872. current->trace_recursion--;
  1873. }
  1874. #else
  1875. #define trace_recursive_lock() (0)
  1876. #define trace_recursive_unlock() do { } while (0)
  1877. #endif
  1878. static DEFINE_PER_CPU(int, rb_need_resched);
  1879. /**
  1880. * ring_buffer_lock_reserve - reserve a part of the buffer
  1881. * @buffer: the ring buffer to reserve from
  1882. * @length: the length of the data to reserve (excluding event header)
  1883. *
  1884. * Returns a reseverd event on the ring buffer to copy directly to.
  1885. * The user of this interface will need to get the body to write into
  1886. * and can use the ring_buffer_event_data() interface.
  1887. *
  1888. * The length is the length of the data needed, not the event length
  1889. * which also includes the event header.
  1890. *
  1891. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1892. * If NULL is returned, then nothing has been allocated or locked.
  1893. */
  1894. struct ring_buffer_event *
  1895. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1896. {
  1897. struct ring_buffer_per_cpu *cpu_buffer;
  1898. struct ring_buffer_event *event;
  1899. int cpu, resched;
  1900. if (ring_buffer_flags != RB_BUFFERS_ON)
  1901. return NULL;
  1902. if (atomic_read(&buffer->record_disabled))
  1903. return NULL;
  1904. /* If we are tracing schedule, we don't want to recurse */
  1905. resched = ftrace_preempt_disable();
  1906. if (trace_recursive_lock())
  1907. goto out_nocheck;
  1908. cpu = raw_smp_processor_id();
  1909. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1910. goto out;
  1911. cpu_buffer = buffer->buffers[cpu];
  1912. if (atomic_read(&cpu_buffer->record_disabled))
  1913. goto out;
  1914. if (length > BUF_MAX_DATA_SIZE)
  1915. goto out;
  1916. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  1917. if (!event)
  1918. goto out;
  1919. /*
  1920. * Need to store resched state on this cpu.
  1921. * Only the first needs to.
  1922. */
  1923. if (preempt_count() == 1)
  1924. per_cpu(rb_need_resched, cpu) = resched;
  1925. return event;
  1926. out:
  1927. trace_recursive_unlock();
  1928. out_nocheck:
  1929. ftrace_preempt_enable(resched);
  1930. return NULL;
  1931. }
  1932. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1933. static void
  1934. rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1935. struct ring_buffer_event *event)
  1936. {
  1937. /*
  1938. * The event first in the commit queue updates the
  1939. * time stamp.
  1940. */
  1941. if (rb_event_is_commit(cpu_buffer, event))
  1942. cpu_buffer->write_stamp += event->time_delta;
  1943. }
  1944. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1945. struct ring_buffer_event *event)
  1946. {
  1947. local_inc(&cpu_buffer->entries);
  1948. rb_update_write_stamp(cpu_buffer, event);
  1949. rb_end_commit(cpu_buffer);
  1950. }
  1951. /**
  1952. * ring_buffer_unlock_commit - commit a reserved
  1953. * @buffer: The buffer to commit to
  1954. * @event: The event pointer to commit.
  1955. *
  1956. * This commits the data to the ring buffer, and releases any locks held.
  1957. *
  1958. * Must be paired with ring_buffer_lock_reserve.
  1959. */
  1960. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1961. struct ring_buffer_event *event)
  1962. {
  1963. struct ring_buffer_per_cpu *cpu_buffer;
  1964. int cpu = raw_smp_processor_id();
  1965. cpu_buffer = buffer->buffers[cpu];
  1966. rb_commit(cpu_buffer, event);
  1967. trace_recursive_unlock();
  1968. /*
  1969. * Only the last preempt count needs to restore preemption.
  1970. */
  1971. if (preempt_count() == 1)
  1972. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1973. else
  1974. preempt_enable_no_resched_notrace();
  1975. return 0;
  1976. }
  1977. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1978. static inline void rb_event_discard(struct ring_buffer_event *event)
  1979. {
  1980. /* array[0] holds the actual length for the discarded event */
  1981. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  1982. event->type_len = RINGBUF_TYPE_PADDING;
  1983. /* time delta must be non zero */
  1984. if (!event->time_delta)
  1985. event->time_delta = 1;
  1986. }
  1987. /*
  1988. * Decrement the entries to the page that an event is on.
  1989. * The event does not even need to exist, only the pointer
  1990. * to the page it is on. This may only be called before the commit
  1991. * takes place.
  1992. */
  1993. static inline void
  1994. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  1995. struct ring_buffer_event *event)
  1996. {
  1997. unsigned long addr = (unsigned long)event;
  1998. struct buffer_page *bpage = cpu_buffer->commit_page;
  1999. struct buffer_page *start;
  2000. addr &= PAGE_MASK;
  2001. /* Do the likely case first */
  2002. if (likely(bpage->page == (void *)addr)) {
  2003. local_dec(&bpage->entries);
  2004. return;
  2005. }
  2006. /*
  2007. * Because the commit page may be on the reader page we
  2008. * start with the next page and check the end loop there.
  2009. */
  2010. rb_inc_page(cpu_buffer, &bpage);
  2011. start = bpage;
  2012. do {
  2013. if (bpage->page == (void *)addr) {
  2014. local_dec(&bpage->entries);
  2015. return;
  2016. }
  2017. rb_inc_page(cpu_buffer, &bpage);
  2018. } while (bpage != start);
  2019. /* commit not part of this buffer?? */
  2020. RB_WARN_ON(cpu_buffer, 1);
  2021. }
  2022. /**
  2023. * ring_buffer_commit_discard - discard an event that has not been committed
  2024. * @buffer: the ring buffer
  2025. * @event: non committed event to discard
  2026. *
  2027. * Sometimes an event that is in the ring buffer needs to be ignored.
  2028. * This function lets the user discard an event in the ring buffer
  2029. * and then that event will not be read later.
  2030. *
  2031. * This function only works if it is called before the the item has been
  2032. * committed. It will try to free the event from the ring buffer
  2033. * if another event has not been added behind it.
  2034. *
  2035. * If another event has been added behind it, it will set the event
  2036. * up as discarded, and perform the commit.
  2037. *
  2038. * If this function is called, do not call ring_buffer_unlock_commit on
  2039. * the event.
  2040. */
  2041. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  2042. struct ring_buffer_event *event)
  2043. {
  2044. struct ring_buffer_per_cpu *cpu_buffer;
  2045. int cpu;
  2046. /* The event is discarded regardless */
  2047. rb_event_discard(event);
  2048. cpu = smp_processor_id();
  2049. cpu_buffer = buffer->buffers[cpu];
  2050. /*
  2051. * This must only be called if the event has not been
  2052. * committed yet. Thus we can assume that preemption
  2053. * is still disabled.
  2054. */
  2055. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  2056. rb_decrement_entry(cpu_buffer, event);
  2057. if (rb_try_to_discard(cpu_buffer, event))
  2058. goto out;
  2059. /*
  2060. * The commit is still visible by the reader, so we
  2061. * must still update the timestamp.
  2062. */
  2063. rb_update_write_stamp(cpu_buffer, event);
  2064. out:
  2065. rb_end_commit(cpu_buffer);
  2066. trace_recursive_unlock();
  2067. /*
  2068. * Only the last preempt count needs to restore preemption.
  2069. */
  2070. if (preempt_count() == 1)
  2071. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  2072. else
  2073. preempt_enable_no_resched_notrace();
  2074. }
  2075. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  2076. /**
  2077. * ring_buffer_write - write data to the buffer without reserving
  2078. * @buffer: The ring buffer to write to.
  2079. * @length: The length of the data being written (excluding the event header)
  2080. * @data: The data to write to the buffer.
  2081. *
  2082. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  2083. * one function. If you already have the data to write to the buffer, it
  2084. * may be easier to simply call this function.
  2085. *
  2086. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  2087. * and not the length of the event which would hold the header.
  2088. */
  2089. int ring_buffer_write(struct ring_buffer *buffer,
  2090. unsigned long length,
  2091. void *data)
  2092. {
  2093. struct ring_buffer_per_cpu *cpu_buffer;
  2094. struct ring_buffer_event *event;
  2095. void *body;
  2096. int ret = -EBUSY;
  2097. int cpu, resched;
  2098. if (ring_buffer_flags != RB_BUFFERS_ON)
  2099. return -EBUSY;
  2100. if (atomic_read(&buffer->record_disabled))
  2101. return -EBUSY;
  2102. resched = ftrace_preempt_disable();
  2103. cpu = raw_smp_processor_id();
  2104. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2105. goto out;
  2106. cpu_buffer = buffer->buffers[cpu];
  2107. if (atomic_read(&cpu_buffer->record_disabled))
  2108. goto out;
  2109. if (length > BUF_MAX_DATA_SIZE)
  2110. goto out;
  2111. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  2112. if (!event)
  2113. goto out;
  2114. body = rb_event_data(event);
  2115. memcpy(body, data, length);
  2116. rb_commit(cpu_buffer, event);
  2117. ret = 0;
  2118. out:
  2119. ftrace_preempt_enable(resched);
  2120. return ret;
  2121. }
  2122. EXPORT_SYMBOL_GPL(ring_buffer_write);
  2123. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  2124. {
  2125. struct buffer_page *reader = cpu_buffer->reader_page;
  2126. struct buffer_page *head = rb_set_head_page(cpu_buffer);
  2127. struct buffer_page *commit = cpu_buffer->commit_page;
  2128. /* In case of error, head will be NULL */
  2129. if (unlikely(!head))
  2130. return 1;
  2131. return reader->read == rb_page_commit(reader) &&
  2132. (commit == reader ||
  2133. (commit == head &&
  2134. head->read == rb_page_commit(commit)));
  2135. }
  2136. /**
  2137. * ring_buffer_record_disable - stop all writes into the buffer
  2138. * @buffer: The ring buffer to stop writes to.
  2139. *
  2140. * This prevents all writes to the buffer. Any attempt to write
  2141. * to the buffer after this will fail and return NULL.
  2142. *
  2143. * The caller should call synchronize_sched() after this.
  2144. */
  2145. void ring_buffer_record_disable(struct ring_buffer *buffer)
  2146. {
  2147. atomic_inc(&buffer->record_disabled);
  2148. }
  2149. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  2150. /**
  2151. * ring_buffer_record_enable - enable writes to the buffer
  2152. * @buffer: The ring buffer to enable writes
  2153. *
  2154. * Note, multiple disables will need the same number of enables
  2155. * to truely enable the writing (much like preempt_disable).
  2156. */
  2157. void ring_buffer_record_enable(struct ring_buffer *buffer)
  2158. {
  2159. atomic_dec(&buffer->record_disabled);
  2160. }
  2161. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  2162. /**
  2163. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  2164. * @buffer: The ring buffer to stop writes to.
  2165. * @cpu: The CPU buffer to stop
  2166. *
  2167. * This prevents all writes to the buffer. Any attempt to write
  2168. * to the buffer after this will fail and return NULL.
  2169. *
  2170. * The caller should call synchronize_sched() after this.
  2171. */
  2172. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  2173. {
  2174. struct ring_buffer_per_cpu *cpu_buffer;
  2175. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2176. return;
  2177. cpu_buffer = buffer->buffers[cpu];
  2178. atomic_inc(&cpu_buffer->record_disabled);
  2179. }
  2180. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  2181. /**
  2182. * ring_buffer_record_enable_cpu - enable writes to the buffer
  2183. * @buffer: The ring buffer to enable writes
  2184. * @cpu: The CPU to enable.
  2185. *
  2186. * Note, multiple disables will need the same number of enables
  2187. * to truely enable the writing (much like preempt_disable).
  2188. */
  2189. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  2190. {
  2191. struct ring_buffer_per_cpu *cpu_buffer;
  2192. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2193. return;
  2194. cpu_buffer = buffer->buffers[cpu];
  2195. atomic_dec(&cpu_buffer->record_disabled);
  2196. }
  2197. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  2198. /**
  2199. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  2200. * @buffer: The ring buffer
  2201. * @cpu: The per CPU buffer to get the entries from.
  2202. */
  2203. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  2204. {
  2205. struct ring_buffer_per_cpu *cpu_buffer;
  2206. unsigned long ret;
  2207. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2208. return 0;
  2209. cpu_buffer = buffer->buffers[cpu];
  2210. ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
  2211. - cpu_buffer->read;
  2212. return ret;
  2213. }
  2214. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  2215. /**
  2216. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  2217. * @buffer: The ring buffer
  2218. * @cpu: The per CPU buffer to get the number of overruns from
  2219. */
  2220. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2221. {
  2222. struct ring_buffer_per_cpu *cpu_buffer;
  2223. unsigned long ret;
  2224. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2225. return 0;
  2226. cpu_buffer = buffer->buffers[cpu];
  2227. ret = local_read(&cpu_buffer->overrun);
  2228. return ret;
  2229. }
  2230. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  2231. /**
  2232. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  2233. * @buffer: The ring buffer
  2234. * @cpu: The per CPU buffer to get the number of overruns from
  2235. */
  2236. unsigned long
  2237. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2238. {
  2239. struct ring_buffer_per_cpu *cpu_buffer;
  2240. unsigned long ret;
  2241. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2242. return 0;
  2243. cpu_buffer = buffer->buffers[cpu];
  2244. ret = local_read(&cpu_buffer->commit_overrun);
  2245. return ret;
  2246. }
  2247. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  2248. /**
  2249. * ring_buffer_entries - get the number of entries in a buffer
  2250. * @buffer: The ring buffer
  2251. *
  2252. * Returns the total number of entries in the ring buffer
  2253. * (all CPU entries)
  2254. */
  2255. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  2256. {
  2257. struct ring_buffer_per_cpu *cpu_buffer;
  2258. unsigned long entries = 0;
  2259. int cpu;
  2260. /* if you care about this being correct, lock the buffer */
  2261. for_each_buffer_cpu(buffer, cpu) {
  2262. cpu_buffer = buffer->buffers[cpu];
  2263. entries += (local_read(&cpu_buffer->entries) -
  2264. local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
  2265. }
  2266. return entries;
  2267. }
  2268. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  2269. /**
  2270. * ring_buffer_overruns - get the number of overruns in buffer
  2271. * @buffer: The ring buffer
  2272. *
  2273. * Returns the total number of overruns in the ring buffer
  2274. * (all CPU entries)
  2275. */
  2276. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  2277. {
  2278. struct ring_buffer_per_cpu *cpu_buffer;
  2279. unsigned long overruns = 0;
  2280. int cpu;
  2281. /* if you care about this being correct, lock the buffer */
  2282. for_each_buffer_cpu(buffer, cpu) {
  2283. cpu_buffer = buffer->buffers[cpu];
  2284. overruns += local_read(&cpu_buffer->overrun);
  2285. }
  2286. return overruns;
  2287. }
  2288. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  2289. static void rb_iter_reset(struct ring_buffer_iter *iter)
  2290. {
  2291. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2292. /* Iterator usage is expected to have record disabled */
  2293. if (list_empty(&cpu_buffer->reader_page->list)) {
  2294. iter->head_page = rb_set_head_page(cpu_buffer);
  2295. if (unlikely(!iter->head_page))
  2296. return;
  2297. iter->head = iter->head_page->read;
  2298. } else {
  2299. iter->head_page = cpu_buffer->reader_page;
  2300. iter->head = cpu_buffer->reader_page->read;
  2301. }
  2302. if (iter->head)
  2303. iter->read_stamp = cpu_buffer->read_stamp;
  2304. else
  2305. iter->read_stamp = iter->head_page->page->time_stamp;
  2306. }
  2307. /**
  2308. * ring_buffer_iter_reset - reset an iterator
  2309. * @iter: The iterator to reset
  2310. *
  2311. * Resets the iterator, so that it will start from the beginning
  2312. * again.
  2313. */
  2314. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  2315. {
  2316. struct ring_buffer_per_cpu *cpu_buffer;
  2317. unsigned long flags;
  2318. if (!iter)
  2319. return;
  2320. cpu_buffer = iter->cpu_buffer;
  2321. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2322. rb_iter_reset(iter);
  2323. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2324. }
  2325. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  2326. /**
  2327. * ring_buffer_iter_empty - check if an iterator has no more to read
  2328. * @iter: The iterator to check
  2329. */
  2330. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  2331. {
  2332. struct ring_buffer_per_cpu *cpu_buffer;
  2333. cpu_buffer = iter->cpu_buffer;
  2334. return iter->head_page == cpu_buffer->commit_page &&
  2335. iter->head == rb_commit_index(cpu_buffer);
  2336. }
  2337. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  2338. static void
  2339. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2340. struct ring_buffer_event *event)
  2341. {
  2342. u64 delta;
  2343. switch (event->type_len) {
  2344. case RINGBUF_TYPE_PADDING:
  2345. return;
  2346. case RINGBUF_TYPE_TIME_EXTEND:
  2347. delta = event->array[0];
  2348. delta <<= TS_SHIFT;
  2349. delta += event->time_delta;
  2350. cpu_buffer->read_stamp += delta;
  2351. return;
  2352. case RINGBUF_TYPE_TIME_STAMP:
  2353. /* FIXME: not implemented */
  2354. return;
  2355. case RINGBUF_TYPE_DATA:
  2356. cpu_buffer->read_stamp += event->time_delta;
  2357. return;
  2358. default:
  2359. BUG();
  2360. }
  2361. return;
  2362. }
  2363. static void
  2364. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  2365. struct ring_buffer_event *event)
  2366. {
  2367. u64 delta;
  2368. switch (event->type_len) {
  2369. case RINGBUF_TYPE_PADDING:
  2370. return;
  2371. case RINGBUF_TYPE_TIME_EXTEND:
  2372. delta = event->array[0];
  2373. delta <<= TS_SHIFT;
  2374. delta += event->time_delta;
  2375. iter->read_stamp += delta;
  2376. return;
  2377. case RINGBUF_TYPE_TIME_STAMP:
  2378. /* FIXME: not implemented */
  2379. return;
  2380. case RINGBUF_TYPE_DATA:
  2381. iter->read_stamp += event->time_delta;
  2382. return;
  2383. default:
  2384. BUG();
  2385. }
  2386. return;
  2387. }
  2388. static struct buffer_page *
  2389. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  2390. {
  2391. struct buffer_page *reader = NULL;
  2392. unsigned long flags;
  2393. int nr_loops = 0;
  2394. int ret;
  2395. local_irq_save(flags);
  2396. __raw_spin_lock(&cpu_buffer->lock);
  2397. again:
  2398. /*
  2399. * This should normally only loop twice. But because the
  2400. * start of the reader inserts an empty page, it causes
  2401. * a case where we will loop three times. There should be no
  2402. * reason to loop four times (that I know of).
  2403. */
  2404. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  2405. reader = NULL;
  2406. goto out;
  2407. }
  2408. reader = cpu_buffer->reader_page;
  2409. /* If there's more to read, return this page */
  2410. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  2411. goto out;
  2412. /* Never should we have an index greater than the size */
  2413. if (RB_WARN_ON(cpu_buffer,
  2414. cpu_buffer->reader_page->read > rb_page_size(reader)))
  2415. goto out;
  2416. /* check if we caught up to the tail */
  2417. reader = NULL;
  2418. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  2419. goto out;
  2420. /*
  2421. * Reset the reader page to size zero.
  2422. */
  2423. local_set(&cpu_buffer->reader_page->write, 0);
  2424. local_set(&cpu_buffer->reader_page->entries, 0);
  2425. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2426. spin:
  2427. /*
  2428. * Splice the empty reader page into the list around the head.
  2429. */
  2430. reader = rb_set_head_page(cpu_buffer);
  2431. cpu_buffer->reader_page->list.next = reader->list.next;
  2432. cpu_buffer->reader_page->list.prev = reader->list.prev;
  2433. /*
  2434. * cpu_buffer->pages just needs to point to the buffer, it
  2435. * has no specific buffer page to point to. Lets move it out
  2436. * of our way so we don't accidently swap it.
  2437. */
  2438. cpu_buffer->pages = reader->list.prev;
  2439. /* The reader page will be pointing to the new head */
  2440. rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
  2441. /*
  2442. * Here's the tricky part.
  2443. *
  2444. * We need to move the pointer past the header page.
  2445. * But we can only do that if a writer is not currently
  2446. * moving it. The page before the header page has the
  2447. * flag bit '1' set if it is pointing to the page we want.
  2448. * but if the writer is in the process of moving it
  2449. * than it will be '2' or already moved '0'.
  2450. */
  2451. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  2452. /*
  2453. * If we did not convert it, then we must try again.
  2454. */
  2455. if (!ret)
  2456. goto spin;
  2457. /*
  2458. * Yeah! We succeeded in replacing the page.
  2459. *
  2460. * Now make the new head point back to the reader page.
  2461. */
  2462. reader->list.next->prev = &cpu_buffer->reader_page->list;
  2463. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  2464. /* Finally update the reader page to the new head */
  2465. cpu_buffer->reader_page = reader;
  2466. rb_reset_reader_page(cpu_buffer);
  2467. goto again;
  2468. out:
  2469. __raw_spin_unlock(&cpu_buffer->lock);
  2470. local_irq_restore(flags);
  2471. return reader;
  2472. }
  2473. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  2474. {
  2475. struct ring_buffer_event *event;
  2476. struct buffer_page *reader;
  2477. unsigned length;
  2478. reader = rb_get_reader_page(cpu_buffer);
  2479. /* This function should not be called when buffer is empty */
  2480. if (RB_WARN_ON(cpu_buffer, !reader))
  2481. return;
  2482. event = rb_reader_event(cpu_buffer);
  2483. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  2484. cpu_buffer->read++;
  2485. rb_update_read_stamp(cpu_buffer, event);
  2486. length = rb_event_length(event);
  2487. cpu_buffer->reader_page->read += length;
  2488. }
  2489. static void rb_advance_iter(struct ring_buffer_iter *iter)
  2490. {
  2491. struct ring_buffer *buffer;
  2492. struct ring_buffer_per_cpu *cpu_buffer;
  2493. struct ring_buffer_event *event;
  2494. unsigned length;
  2495. cpu_buffer = iter->cpu_buffer;
  2496. buffer = cpu_buffer->buffer;
  2497. /*
  2498. * Check if we are at the end of the buffer.
  2499. */
  2500. if (iter->head >= rb_page_size(iter->head_page)) {
  2501. /* discarded commits can make the page empty */
  2502. if (iter->head_page == cpu_buffer->commit_page)
  2503. return;
  2504. rb_inc_iter(iter);
  2505. return;
  2506. }
  2507. event = rb_iter_head_event(iter);
  2508. length = rb_event_length(event);
  2509. /*
  2510. * This should not be called to advance the header if we are
  2511. * at the tail of the buffer.
  2512. */
  2513. if (RB_WARN_ON(cpu_buffer,
  2514. (iter->head_page == cpu_buffer->commit_page) &&
  2515. (iter->head + length > rb_commit_index(cpu_buffer))))
  2516. return;
  2517. rb_update_iter_read_stamp(iter, event);
  2518. iter->head += length;
  2519. /* check for end of page padding */
  2520. if ((iter->head >= rb_page_size(iter->head_page)) &&
  2521. (iter->head_page != cpu_buffer->commit_page))
  2522. rb_advance_iter(iter);
  2523. }
  2524. static struct ring_buffer_event *
  2525. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
  2526. {
  2527. struct ring_buffer_event *event;
  2528. struct buffer_page *reader;
  2529. int nr_loops = 0;
  2530. again:
  2531. /*
  2532. * We repeat when a timestamp is encountered. It is possible
  2533. * to get multiple timestamps from an interrupt entering just
  2534. * as one timestamp is about to be written, or from discarded
  2535. * commits. The most that we can have is the number on a single page.
  2536. */
  2537. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2538. return NULL;
  2539. reader = rb_get_reader_page(cpu_buffer);
  2540. if (!reader)
  2541. return NULL;
  2542. event = rb_reader_event(cpu_buffer);
  2543. switch (event->type_len) {
  2544. case RINGBUF_TYPE_PADDING:
  2545. if (rb_null_event(event))
  2546. RB_WARN_ON(cpu_buffer, 1);
  2547. /*
  2548. * Because the writer could be discarding every
  2549. * event it creates (which would probably be bad)
  2550. * if we were to go back to "again" then we may never
  2551. * catch up, and will trigger the warn on, or lock
  2552. * the box. Return the padding, and we will release
  2553. * the current locks, and try again.
  2554. */
  2555. return event;
  2556. case RINGBUF_TYPE_TIME_EXTEND:
  2557. /* Internal data, OK to advance */
  2558. rb_advance_reader(cpu_buffer);
  2559. goto again;
  2560. case RINGBUF_TYPE_TIME_STAMP:
  2561. /* FIXME: not implemented */
  2562. rb_advance_reader(cpu_buffer);
  2563. goto again;
  2564. case RINGBUF_TYPE_DATA:
  2565. if (ts) {
  2566. *ts = cpu_buffer->read_stamp + event->time_delta;
  2567. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  2568. cpu_buffer->cpu, ts);
  2569. }
  2570. return event;
  2571. default:
  2572. BUG();
  2573. }
  2574. return NULL;
  2575. }
  2576. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  2577. static struct ring_buffer_event *
  2578. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2579. {
  2580. struct ring_buffer *buffer;
  2581. struct ring_buffer_per_cpu *cpu_buffer;
  2582. struct ring_buffer_event *event;
  2583. int nr_loops = 0;
  2584. if (ring_buffer_iter_empty(iter))
  2585. return NULL;
  2586. cpu_buffer = iter->cpu_buffer;
  2587. buffer = cpu_buffer->buffer;
  2588. again:
  2589. /*
  2590. * We repeat when a timestamp is encountered.
  2591. * We can get multiple timestamps by nested interrupts or also
  2592. * if filtering is on (discarding commits). Since discarding
  2593. * commits can be frequent we can get a lot of timestamps.
  2594. * But we limit them by not adding timestamps if they begin
  2595. * at the start of a page.
  2596. */
  2597. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2598. return NULL;
  2599. if (rb_per_cpu_empty(cpu_buffer))
  2600. return NULL;
  2601. event = rb_iter_head_event(iter);
  2602. switch (event->type_len) {
  2603. case RINGBUF_TYPE_PADDING:
  2604. if (rb_null_event(event)) {
  2605. rb_inc_iter(iter);
  2606. goto again;
  2607. }
  2608. rb_advance_iter(iter);
  2609. return event;
  2610. case RINGBUF_TYPE_TIME_EXTEND:
  2611. /* Internal data, OK to advance */
  2612. rb_advance_iter(iter);
  2613. goto again;
  2614. case RINGBUF_TYPE_TIME_STAMP:
  2615. /* FIXME: not implemented */
  2616. rb_advance_iter(iter);
  2617. goto again;
  2618. case RINGBUF_TYPE_DATA:
  2619. if (ts) {
  2620. *ts = iter->read_stamp + event->time_delta;
  2621. ring_buffer_normalize_time_stamp(buffer,
  2622. cpu_buffer->cpu, ts);
  2623. }
  2624. return event;
  2625. default:
  2626. BUG();
  2627. }
  2628. return NULL;
  2629. }
  2630. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2631. static inline int rb_ok_to_lock(void)
  2632. {
  2633. /*
  2634. * If an NMI die dumps out the content of the ring buffer
  2635. * do not grab locks. We also permanently disable the ring
  2636. * buffer too. A one time deal is all you get from reading
  2637. * the ring buffer from an NMI.
  2638. */
  2639. if (likely(!in_nmi()))
  2640. return 1;
  2641. tracing_off_permanent();
  2642. return 0;
  2643. }
  2644. /**
  2645. * ring_buffer_peek - peek at the next event to be read
  2646. * @buffer: The ring buffer to read
  2647. * @cpu: The cpu to peak at
  2648. * @ts: The timestamp counter of this event.
  2649. *
  2650. * This will return the event that will be read next, but does
  2651. * not consume the data.
  2652. */
  2653. struct ring_buffer_event *
  2654. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  2655. {
  2656. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2657. struct ring_buffer_event *event;
  2658. unsigned long flags;
  2659. int dolock;
  2660. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2661. return NULL;
  2662. dolock = rb_ok_to_lock();
  2663. again:
  2664. local_irq_save(flags);
  2665. if (dolock)
  2666. spin_lock(&cpu_buffer->reader_lock);
  2667. event = rb_buffer_peek(cpu_buffer, ts);
  2668. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2669. rb_advance_reader(cpu_buffer);
  2670. if (dolock)
  2671. spin_unlock(&cpu_buffer->reader_lock);
  2672. local_irq_restore(flags);
  2673. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2674. goto again;
  2675. return event;
  2676. }
  2677. /**
  2678. * ring_buffer_iter_peek - peek at the next event to be read
  2679. * @iter: The ring buffer iterator
  2680. * @ts: The timestamp counter of this event.
  2681. *
  2682. * This will return the event that will be read next, but does
  2683. * not increment the iterator.
  2684. */
  2685. struct ring_buffer_event *
  2686. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2687. {
  2688. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2689. struct ring_buffer_event *event;
  2690. unsigned long flags;
  2691. again:
  2692. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2693. event = rb_iter_peek(iter, ts);
  2694. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2695. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2696. goto again;
  2697. return event;
  2698. }
  2699. /**
  2700. * ring_buffer_consume - return an event and consume it
  2701. * @buffer: The ring buffer to get the next event from
  2702. *
  2703. * Returns the next event in the ring buffer, and that event is consumed.
  2704. * Meaning, that sequential reads will keep returning a different event,
  2705. * and eventually empty the ring buffer if the producer is slower.
  2706. */
  2707. struct ring_buffer_event *
  2708. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2709. {
  2710. struct ring_buffer_per_cpu *cpu_buffer;
  2711. struct ring_buffer_event *event = NULL;
  2712. unsigned long flags;
  2713. int dolock;
  2714. dolock = rb_ok_to_lock();
  2715. again:
  2716. /* might be called in atomic */
  2717. preempt_disable();
  2718. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2719. goto out;
  2720. cpu_buffer = buffer->buffers[cpu];
  2721. local_irq_save(flags);
  2722. if (dolock)
  2723. spin_lock(&cpu_buffer->reader_lock);
  2724. event = rb_buffer_peek(cpu_buffer, ts);
  2725. if (event)
  2726. rb_advance_reader(cpu_buffer);
  2727. if (dolock)
  2728. spin_unlock(&cpu_buffer->reader_lock);
  2729. local_irq_restore(flags);
  2730. out:
  2731. preempt_enable();
  2732. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2733. goto again;
  2734. return event;
  2735. }
  2736. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2737. /**
  2738. * ring_buffer_read_start - start a non consuming read of the buffer
  2739. * @buffer: The ring buffer to read from
  2740. * @cpu: The cpu buffer to iterate over
  2741. *
  2742. * This starts up an iteration through the buffer. It also disables
  2743. * the recording to the buffer until the reading is finished.
  2744. * This prevents the reading from being corrupted. This is not
  2745. * a consuming read, so a producer is not expected.
  2746. *
  2747. * Must be paired with ring_buffer_finish.
  2748. */
  2749. struct ring_buffer_iter *
  2750. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2751. {
  2752. struct ring_buffer_per_cpu *cpu_buffer;
  2753. struct ring_buffer_iter *iter;
  2754. unsigned long flags;
  2755. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2756. return NULL;
  2757. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2758. if (!iter)
  2759. return NULL;
  2760. cpu_buffer = buffer->buffers[cpu];
  2761. iter->cpu_buffer = cpu_buffer;
  2762. atomic_inc(&cpu_buffer->record_disabled);
  2763. synchronize_sched();
  2764. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2765. __raw_spin_lock(&cpu_buffer->lock);
  2766. rb_iter_reset(iter);
  2767. __raw_spin_unlock(&cpu_buffer->lock);
  2768. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2769. return iter;
  2770. }
  2771. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2772. /**
  2773. * ring_buffer_finish - finish reading the iterator of the buffer
  2774. * @iter: The iterator retrieved by ring_buffer_start
  2775. *
  2776. * This re-enables the recording to the buffer, and frees the
  2777. * iterator.
  2778. */
  2779. void
  2780. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2781. {
  2782. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2783. atomic_dec(&cpu_buffer->record_disabled);
  2784. kfree(iter);
  2785. }
  2786. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2787. /**
  2788. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2789. * @iter: The ring buffer iterator
  2790. * @ts: The time stamp of the event read.
  2791. *
  2792. * This reads the next event in the ring buffer and increments the iterator.
  2793. */
  2794. struct ring_buffer_event *
  2795. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2796. {
  2797. struct ring_buffer_event *event;
  2798. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2799. unsigned long flags;
  2800. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2801. again:
  2802. event = rb_iter_peek(iter, ts);
  2803. if (!event)
  2804. goto out;
  2805. if (event->type_len == RINGBUF_TYPE_PADDING)
  2806. goto again;
  2807. rb_advance_iter(iter);
  2808. out:
  2809. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2810. return event;
  2811. }
  2812. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2813. /**
  2814. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2815. * @buffer: The ring buffer.
  2816. */
  2817. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2818. {
  2819. return BUF_PAGE_SIZE * buffer->pages;
  2820. }
  2821. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2822. static void
  2823. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2824. {
  2825. rb_head_page_deactivate(cpu_buffer);
  2826. cpu_buffer->head_page
  2827. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  2828. local_set(&cpu_buffer->head_page->write, 0);
  2829. local_set(&cpu_buffer->head_page->entries, 0);
  2830. local_set(&cpu_buffer->head_page->page->commit, 0);
  2831. cpu_buffer->head_page->read = 0;
  2832. cpu_buffer->tail_page = cpu_buffer->head_page;
  2833. cpu_buffer->commit_page = cpu_buffer->head_page;
  2834. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2835. local_set(&cpu_buffer->reader_page->write, 0);
  2836. local_set(&cpu_buffer->reader_page->entries, 0);
  2837. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2838. cpu_buffer->reader_page->read = 0;
  2839. local_set(&cpu_buffer->commit_overrun, 0);
  2840. local_set(&cpu_buffer->overrun, 0);
  2841. local_set(&cpu_buffer->entries, 0);
  2842. local_set(&cpu_buffer->committing, 0);
  2843. local_set(&cpu_buffer->commits, 0);
  2844. cpu_buffer->read = 0;
  2845. cpu_buffer->write_stamp = 0;
  2846. cpu_buffer->read_stamp = 0;
  2847. rb_head_page_activate(cpu_buffer);
  2848. }
  2849. /**
  2850. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2851. * @buffer: The ring buffer to reset a per cpu buffer of
  2852. * @cpu: The CPU buffer to be reset
  2853. */
  2854. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2855. {
  2856. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2857. unsigned long flags;
  2858. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2859. return;
  2860. atomic_inc(&cpu_buffer->record_disabled);
  2861. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2862. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  2863. goto out;
  2864. __raw_spin_lock(&cpu_buffer->lock);
  2865. rb_reset_cpu(cpu_buffer);
  2866. __raw_spin_unlock(&cpu_buffer->lock);
  2867. out:
  2868. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2869. atomic_dec(&cpu_buffer->record_disabled);
  2870. }
  2871. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2872. /**
  2873. * ring_buffer_reset - reset a ring buffer
  2874. * @buffer: The ring buffer to reset all cpu buffers
  2875. */
  2876. void ring_buffer_reset(struct ring_buffer *buffer)
  2877. {
  2878. int cpu;
  2879. for_each_buffer_cpu(buffer, cpu)
  2880. ring_buffer_reset_cpu(buffer, cpu);
  2881. }
  2882. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2883. /**
  2884. * rind_buffer_empty - is the ring buffer empty?
  2885. * @buffer: The ring buffer to test
  2886. */
  2887. int ring_buffer_empty(struct ring_buffer *buffer)
  2888. {
  2889. struct ring_buffer_per_cpu *cpu_buffer;
  2890. unsigned long flags;
  2891. int dolock;
  2892. int cpu;
  2893. int ret;
  2894. dolock = rb_ok_to_lock();
  2895. /* yes this is racy, but if you don't like the race, lock the buffer */
  2896. for_each_buffer_cpu(buffer, cpu) {
  2897. cpu_buffer = buffer->buffers[cpu];
  2898. local_irq_save(flags);
  2899. if (dolock)
  2900. spin_lock(&cpu_buffer->reader_lock);
  2901. ret = rb_per_cpu_empty(cpu_buffer);
  2902. if (dolock)
  2903. spin_unlock(&cpu_buffer->reader_lock);
  2904. local_irq_restore(flags);
  2905. if (!ret)
  2906. return 0;
  2907. }
  2908. return 1;
  2909. }
  2910. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2911. /**
  2912. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2913. * @buffer: The ring buffer
  2914. * @cpu: The CPU buffer to test
  2915. */
  2916. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2917. {
  2918. struct ring_buffer_per_cpu *cpu_buffer;
  2919. unsigned long flags;
  2920. int dolock;
  2921. int ret;
  2922. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2923. return 1;
  2924. dolock = rb_ok_to_lock();
  2925. cpu_buffer = buffer->buffers[cpu];
  2926. local_irq_save(flags);
  2927. if (dolock)
  2928. spin_lock(&cpu_buffer->reader_lock);
  2929. ret = rb_per_cpu_empty(cpu_buffer);
  2930. if (dolock)
  2931. spin_unlock(&cpu_buffer->reader_lock);
  2932. local_irq_restore(flags);
  2933. return ret;
  2934. }
  2935. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2936. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  2937. /**
  2938. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2939. * @buffer_a: One buffer to swap with
  2940. * @buffer_b: The other buffer to swap with
  2941. *
  2942. * This function is useful for tracers that want to take a "snapshot"
  2943. * of a CPU buffer and has another back up buffer lying around.
  2944. * it is expected that the tracer handles the cpu buffer not being
  2945. * used at the moment.
  2946. */
  2947. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2948. struct ring_buffer *buffer_b, int cpu)
  2949. {
  2950. struct ring_buffer_per_cpu *cpu_buffer_a;
  2951. struct ring_buffer_per_cpu *cpu_buffer_b;
  2952. int ret = -EINVAL;
  2953. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2954. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2955. goto out;
  2956. /* At least make sure the two buffers are somewhat the same */
  2957. if (buffer_a->pages != buffer_b->pages)
  2958. goto out;
  2959. ret = -EAGAIN;
  2960. if (ring_buffer_flags != RB_BUFFERS_ON)
  2961. goto out;
  2962. if (atomic_read(&buffer_a->record_disabled))
  2963. goto out;
  2964. if (atomic_read(&buffer_b->record_disabled))
  2965. goto out;
  2966. cpu_buffer_a = buffer_a->buffers[cpu];
  2967. cpu_buffer_b = buffer_b->buffers[cpu];
  2968. if (atomic_read(&cpu_buffer_a->record_disabled))
  2969. goto out;
  2970. if (atomic_read(&cpu_buffer_b->record_disabled))
  2971. goto out;
  2972. /*
  2973. * We can't do a synchronize_sched here because this
  2974. * function can be called in atomic context.
  2975. * Normally this will be called from the same CPU as cpu.
  2976. * If not it's up to the caller to protect this.
  2977. */
  2978. atomic_inc(&cpu_buffer_a->record_disabled);
  2979. atomic_inc(&cpu_buffer_b->record_disabled);
  2980. ret = -EBUSY;
  2981. if (local_read(&cpu_buffer_a->committing))
  2982. goto out_dec;
  2983. if (local_read(&cpu_buffer_b->committing))
  2984. goto out_dec;
  2985. buffer_a->buffers[cpu] = cpu_buffer_b;
  2986. buffer_b->buffers[cpu] = cpu_buffer_a;
  2987. cpu_buffer_b->buffer = buffer_a;
  2988. cpu_buffer_a->buffer = buffer_b;
  2989. ret = 0;
  2990. out_dec:
  2991. atomic_dec(&cpu_buffer_a->record_disabled);
  2992. atomic_dec(&cpu_buffer_b->record_disabled);
  2993. out:
  2994. return ret;
  2995. }
  2996. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2997. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  2998. /**
  2999. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  3000. * @buffer: the buffer to allocate for.
  3001. *
  3002. * This function is used in conjunction with ring_buffer_read_page.
  3003. * When reading a full page from the ring buffer, these functions
  3004. * can be used to speed up the process. The calling function should
  3005. * allocate a few pages first with this function. Then when it
  3006. * needs to get pages from the ring buffer, it passes the result
  3007. * of this function into ring_buffer_read_page, which will swap
  3008. * the page that was allocated, with the read page of the buffer.
  3009. *
  3010. * Returns:
  3011. * The page allocated, or NULL on error.
  3012. */
  3013. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  3014. {
  3015. struct buffer_data_page *bpage;
  3016. unsigned long addr;
  3017. addr = __get_free_page(GFP_KERNEL);
  3018. if (!addr)
  3019. return NULL;
  3020. bpage = (void *)addr;
  3021. rb_init_page(bpage);
  3022. return bpage;
  3023. }
  3024. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  3025. /**
  3026. * ring_buffer_free_read_page - free an allocated read page
  3027. * @buffer: the buffer the page was allocate for
  3028. * @data: the page to free
  3029. *
  3030. * Free a page allocated from ring_buffer_alloc_read_page.
  3031. */
  3032. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  3033. {
  3034. free_page((unsigned long)data);
  3035. }
  3036. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  3037. /**
  3038. * ring_buffer_read_page - extract a page from the ring buffer
  3039. * @buffer: buffer to extract from
  3040. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  3041. * @len: amount to extract
  3042. * @cpu: the cpu of the buffer to extract
  3043. * @full: should the extraction only happen when the page is full.
  3044. *
  3045. * This function will pull out a page from the ring buffer and consume it.
  3046. * @data_page must be the address of the variable that was returned
  3047. * from ring_buffer_alloc_read_page. This is because the page might be used
  3048. * to swap with a page in the ring buffer.
  3049. *
  3050. * for example:
  3051. * rpage = ring_buffer_alloc_read_page(buffer);
  3052. * if (!rpage)
  3053. * return error;
  3054. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  3055. * if (ret >= 0)
  3056. * process_page(rpage, ret);
  3057. *
  3058. * When @full is set, the function will not return true unless
  3059. * the writer is off the reader page.
  3060. *
  3061. * Note: it is up to the calling functions to handle sleeps and wakeups.
  3062. * The ring buffer can be used anywhere in the kernel and can not
  3063. * blindly call wake_up. The layer that uses the ring buffer must be
  3064. * responsible for that.
  3065. *
  3066. * Returns:
  3067. * >=0 if data has been transferred, returns the offset of consumed data.
  3068. * <0 if no data has been transferred.
  3069. */
  3070. int ring_buffer_read_page(struct ring_buffer *buffer,
  3071. void **data_page, size_t len, int cpu, int full)
  3072. {
  3073. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  3074. struct ring_buffer_event *event;
  3075. struct buffer_data_page *bpage;
  3076. struct buffer_page *reader;
  3077. unsigned long flags;
  3078. unsigned int commit;
  3079. unsigned int read;
  3080. u64 save_timestamp;
  3081. int ret = -1;
  3082. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3083. goto out;
  3084. /*
  3085. * If len is not big enough to hold the page header, then
  3086. * we can not copy anything.
  3087. */
  3088. if (len <= BUF_PAGE_HDR_SIZE)
  3089. goto out;
  3090. len -= BUF_PAGE_HDR_SIZE;
  3091. if (!data_page)
  3092. goto out;
  3093. bpage = *data_page;
  3094. if (!bpage)
  3095. goto out;
  3096. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3097. reader = rb_get_reader_page(cpu_buffer);
  3098. if (!reader)
  3099. goto out_unlock;
  3100. event = rb_reader_event(cpu_buffer);
  3101. read = reader->read;
  3102. commit = rb_page_commit(reader);
  3103. /*
  3104. * If this page has been partially read or
  3105. * if len is not big enough to read the rest of the page or
  3106. * a writer is still on the page, then
  3107. * we must copy the data from the page to the buffer.
  3108. * Otherwise, we can simply swap the page with the one passed in.
  3109. */
  3110. if (read || (len < (commit - read)) ||
  3111. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  3112. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  3113. unsigned int rpos = read;
  3114. unsigned int pos = 0;
  3115. unsigned int size;
  3116. if (full)
  3117. goto out_unlock;
  3118. if (len > (commit - read))
  3119. len = (commit - read);
  3120. size = rb_event_length(event);
  3121. if (len < size)
  3122. goto out_unlock;
  3123. /* save the current timestamp, since the user will need it */
  3124. save_timestamp = cpu_buffer->read_stamp;
  3125. /* Need to copy one event at a time */
  3126. do {
  3127. memcpy(bpage->data + pos, rpage->data + rpos, size);
  3128. len -= size;
  3129. rb_advance_reader(cpu_buffer);
  3130. rpos = reader->read;
  3131. pos += size;
  3132. event = rb_reader_event(cpu_buffer);
  3133. size = rb_event_length(event);
  3134. } while (len > size);
  3135. /* update bpage */
  3136. local_set(&bpage->commit, pos);
  3137. bpage->time_stamp = save_timestamp;
  3138. /* we copied everything to the beginning */
  3139. read = 0;
  3140. } else {
  3141. /* update the entry counter */
  3142. cpu_buffer->read += rb_page_entries(reader);
  3143. /* swap the pages */
  3144. rb_init_page(bpage);
  3145. bpage = reader->page;
  3146. reader->page = *data_page;
  3147. local_set(&reader->write, 0);
  3148. local_set(&reader->entries, 0);
  3149. reader->read = 0;
  3150. *data_page = bpage;
  3151. }
  3152. ret = read;
  3153. out_unlock:
  3154. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3155. out:
  3156. return ret;
  3157. }
  3158. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  3159. #ifdef CONFIG_TRACING
  3160. static ssize_t
  3161. rb_simple_read(struct file *filp, char __user *ubuf,
  3162. size_t cnt, loff_t *ppos)
  3163. {
  3164. unsigned long *p = filp->private_data;
  3165. char buf[64];
  3166. int r;
  3167. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  3168. r = sprintf(buf, "permanently disabled\n");
  3169. else
  3170. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  3171. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3172. }
  3173. static ssize_t
  3174. rb_simple_write(struct file *filp, const char __user *ubuf,
  3175. size_t cnt, loff_t *ppos)
  3176. {
  3177. unsigned long *p = filp->private_data;
  3178. char buf[64];
  3179. unsigned long val;
  3180. int ret;
  3181. if (cnt >= sizeof(buf))
  3182. return -EINVAL;
  3183. if (copy_from_user(&buf, ubuf, cnt))
  3184. return -EFAULT;
  3185. buf[cnt] = 0;
  3186. ret = strict_strtoul(buf, 10, &val);
  3187. if (ret < 0)
  3188. return ret;
  3189. if (val)
  3190. set_bit(RB_BUFFERS_ON_BIT, p);
  3191. else
  3192. clear_bit(RB_BUFFERS_ON_BIT, p);
  3193. (*ppos)++;
  3194. return cnt;
  3195. }
  3196. static const struct file_operations rb_simple_fops = {
  3197. .open = tracing_open_generic,
  3198. .read = rb_simple_read,
  3199. .write = rb_simple_write,
  3200. };
  3201. static __init int rb_init_debugfs(void)
  3202. {
  3203. struct dentry *d_tracer;
  3204. d_tracer = tracing_init_dentry();
  3205. trace_create_file("tracing_on", 0644, d_tracer,
  3206. &ring_buffer_flags, &rb_simple_fops);
  3207. return 0;
  3208. }
  3209. fs_initcall(rb_init_debugfs);
  3210. #endif
  3211. #ifdef CONFIG_HOTPLUG_CPU
  3212. static int rb_cpu_notify(struct notifier_block *self,
  3213. unsigned long action, void *hcpu)
  3214. {
  3215. struct ring_buffer *buffer =
  3216. container_of(self, struct ring_buffer, cpu_notify);
  3217. long cpu = (long)hcpu;
  3218. switch (action) {
  3219. case CPU_UP_PREPARE:
  3220. case CPU_UP_PREPARE_FROZEN:
  3221. if (cpumask_test_cpu(cpu, buffer->cpumask))
  3222. return NOTIFY_OK;
  3223. buffer->buffers[cpu] =
  3224. rb_allocate_cpu_buffer(buffer, cpu);
  3225. if (!buffer->buffers[cpu]) {
  3226. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  3227. cpu);
  3228. return NOTIFY_OK;
  3229. }
  3230. smp_wmb();
  3231. cpumask_set_cpu(cpu, buffer->cpumask);
  3232. break;
  3233. case CPU_DOWN_PREPARE:
  3234. case CPU_DOWN_PREPARE_FROZEN:
  3235. /*
  3236. * Do nothing.
  3237. * If we were to free the buffer, then the user would
  3238. * lose any trace that was in the buffer.
  3239. */
  3240. break;
  3241. default:
  3242. break;
  3243. }
  3244. return NOTIFY_OK;
  3245. }
  3246. #endif