ring_buffer.c 113 KB

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