ring_buffer.c 101 KB

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