ring_buffer.c 105 KB

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