ring_buffer.c 102 KB

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