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