ring_buffer.c 96 KB

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