ring_buffer.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339
  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_completion;
  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. rb_head_page_deactivate(cpu_buffer);
  824. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  825. return -1;
  826. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  827. return -1;
  828. if (rb_check_list(cpu_buffer, head))
  829. return -1;
  830. list_for_each_entry_safe(bpage, tmp, head, list) {
  831. if (RB_WARN_ON(cpu_buffer,
  832. bpage->list.next->prev != &bpage->list))
  833. return -1;
  834. if (RB_WARN_ON(cpu_buffer,
  835. bpage->list.prev->next != &bpage->list))
  836. return -1;
  837. if (rb_check_list(cpu_buffer, &bpage->list))
  838. return -1;
  839. }
  840. rb_head_page_activate(cpu_buffer);
  841. return 0;
  842. }
  843. static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
  844. {
  845. int i;
  846. struct buffer_page *bpage, *tmp;
  847. for (i = 0; i < nr_pages; i++) {
  848. struct page *page;
  849. /*
  850. * __GFP_NORETRY flag makes sure that the allocation fails
  851. * gracefully without invoking oom-killer and the system is
  852. * not destabilized.
  853. */
  854. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  855. GFP_KERNEL | __GFP_NORETRY,
  856. cpu_to_node(cpu));
  857. if (!bpage)
  858. goto free_pages;
  859. list_add(&bpage->list, pages);
  860. page = alloc_pages_node(cpu_to_node(cpu),
  861. GFP_KERNEL | __GFP_NORETRY, 0);
  862. if (!page)
  863. goto free_pages;
  864. bpage->page = page_address(page);
  865. rb_init_page(bpage->page);
  866. }
  867. return 0;
  868. free_pages:
  869. list_for_each_entry_safe(bpage, tmp, pages, list) {
  870. list_del_init(&bpage->list);
  871. free_buffer_page(bpage);
  872. }
  873. return -ENOMEM;
  874. }
  875. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  876. unsigned nr_pages)
  877. {
  878. LIST_HEAD(pages);
  879. WARN_ON(!nr_pages);
  880. if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
  881. return -ENOMEM;
  882. /*
  883. * The ring buffer page list is a circular list that does not
  884. * start and end with a list head. All page list items point to
  885. * other pages.
  886. */
  887. cpu_buffer->pages = pages.next;
  888. list_del(&pages);
  889. cpu_buffer->nr_pages = nr_pages;
  890. rb_check_pages(cpu_buffer);
  891. return 0;
  892. }
  893. static struct ring_buffer_per_cpu *
  894. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
  895. {
  896. struct ring_buffer_per_cpu *cpu_buffer;
  897. struct buffer_page *bpage;
  898. struct page *page;
  899. int ret;
  900. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  901. GFP_KERNEL, cpu_to_node(cpu));
  902. if (!cpu_buffer)
  903. return NULL;
  904. cpu_buffer->cpu = cpu;
  905. cpu_buffer->buffer = buffer;
  906. raw_spin_lock_init(&cpu_buffer->reader_lock);
  907. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  908. cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  909. INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
  910. init_completion(&cpu_buffer->update_completion);
  911. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  912. GFP_KERNEL, cpu_to_node(cpu));
  913. if (!bpage)
  914. goto fail_free_buffer;
  915. rb_check_bpage(cpu_buffer, bpage);
  916. cpu_buffer->reader_page = bpage;
  917. page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
  918. if (!page)
  919. goto fail_free_reader;
  920. bpage->page = page_address(page);
  921. rb_init_page(bpage->page);
  922. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  923. ret = rb_allocate_pages(cpu_buffer, nr_pages);
  924. if (ret < 0)
  925. goto fail_free_reader;
  926. cpu_buffer->head_page
  927. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  928. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  929. rb_head_page_activate(cpu_buffer);
  930. return cpu_buffer;
  931. fail_free_reader:
  932. free_buffer_page(cpu_buffer->reader_page);
  933. fail_free_buffer:
  934. kfree(cpu_buffer);
  935. return NULL;
  936. }
  937. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  938. {
  939. struct list_head *head = cpu_buffer->pages;
  940. struct buffer_page *bpage, *tmp;
  941. free_buffer_page(cpu_buffer->reader_page);
  942. rb_head_page_deactivate(cpu_buffer);
  943. if (head) {
  944. list_for_each_entry_safe(bpage, tmp, head, list) {
  945. list_del_init(&bpage->list);
  946. free_buffer_page(bpage);
  947. }
  948. bpage = list_entry(head, struct buffer_page, list);
  949. free_buffer_page(bpage);
  950. }
  951. kfree(cpu_buffer);
  952. }
  953. #ifdef CONFIG_HOTPLUG_CPU
  954. static int rb_cpu_notify(struct notifier_block *self,
  955. unsigned long action, void *hcpu);
  956. #endif
  957. /**
  958. * ring_buffer_alloc - allocate a new ring_buffer
  959. * @size: the size in bytes per cpu that is needed.
  960. * @flags: attributes to set for the ring buffer.
  961. *
  962. * Currently the only flag that is available is the RB_FL_OVERWRITE
  963. * flag. This flag means that the buffer will overwrite old data
  964. * when the buffer wraps. If this flag is not set, the buffer will
  965. * drop data when the tail hits the head.
  966. */
  967. struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  968. struct lock_class_key *key)
  969. {
  970. struct ring_buffer *buffer;
  971. int bsize;
  972. int cpu, nr_pages;
  973. /* keep it in its own cache line */
  974. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  975. GFP_KERNEL);
  976. if (!buffer)
  977. return NULL;
  978. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  979. goto fail_free_buffer;
  980. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  981. buffer->flags = flags;
  982. buffer->clock = trace_clock_local;
  983. buffer->reader_lock_key = key;
  984. /* need at least two pages */
  985. if (nr_pages < 2)
  986. nr_pages = 2;
  987. /*
  988. * In case of non-hotplug cpu, if the ring-buffer is allocated
  989. * in early initcall, it will not be notified of secondary cpus.
  990. * In that off case, we need to allocate for all possible cpus.
  991. */
  992. #ifdef CONFIG_HOTPLUG_CPU
  993. get_online_cpus();
  994. cpumask_copy(buffer->cpumask, cpu_online_mask);
  995. #else
  996. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  997. #endif
  998. buffer->cpus = nr_cpu_ids;
  999. bsize = sizeof(void *) * nr_cpu_ids;
  1000. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  1001. GFP_KERNEL);
  1002. if (!buffer->buffers)
  1003. goto fail_free_cpumask;
  1004. for_each_buffer_cpu(buffer, cpu) {
  1005. buffer->buffers[cpu] =
  1006. rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  1007. if (!buffer->buffers[cpu])
  1008. goto fail_free_buffers;
  1009. }
  1010. #ifdef CONFIG_HOTPLUG_CPU
  1011. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  1012. buffer->cpu_notify.priority = 0;
  1013. register_cpu_notifier(&buffer->cpu_notify);
  1014. #endif
  1015. put_online_cpus();
  1016. mutex_init(&buffer->mutex);
  1017. return buffer;
  1018. fail_free_buffers:
  1019. for_each_buffer_cpu(buffer, cpu) {
  1020. if (buffer->buffers[cpu])
  1021. rb_free_cpu_buffer(buffer->buffers[cpu]);
  1022. }
  1023. kfree(buffer->buffers);
  1024. fail_free_cpumask:
  1025. free_cpumask_var(buffer->cpumask);
  1026. put_online_cpus();
  1027. fail_free_buffer:
  1028. kfree(buffer);
  1029. return NULL;
  1030. }
  1031. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  1032. /**
  1033. * ring_buffer_free - free a ring buffer.
  1034. * @buffer: the buffer to free.
  1035. */
  1036. void
  1037. ring_buffer_free(struct ring_buffer *buffer)
  1038. {
  1039. int cpu;
  1040. get_online_cpus();
  1041. #ifdef CONFIG_HOTPLUG_CPU
  1042. unregister_cpu_notifier(&buffer->cpu_notify);
  1043. #endif
  1044. for_each_buffer_cpu(buffer, cpu)
  1045. rb_free_cpu_buffer(buffer->buffers[cpu]);
  1046. put_online_cpus();
  1047. kfree(buffer->buffers);
  1048. free_cpumask_var(buffer->cpumask);
  1049. kfree(buffer);
  1050. }
  1051. EXPORT_SYMBOL_GPL(ring_buffer_free);
  1052. void ring_buffer_set_clock(struct ring_buffer *buffer,
  1053. u64 (*clock)(void))
  1054. {
  1055. buffer->clock = clock;
  1056. }
  1057. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  1058. static inline unsigned long rb_page_entries(struct buffer_page *bpage)
  1059. {
  1060. return local_read(&bpage->entries) & RB_WRITE_MASK;
  1061. }
  1062. static inline unsigned long rb_page_write(struct buffer_page *bpage)
  1063. {
  1064. return local_read(&bpage->write) & RB_WRITE_MASK;
  1065. }
  1066. static int
  1067. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
  1068. {
  1069. struct list_head *tail_page, *to_remove, *next_page;
  1070. struct buffer_page *to_remove_page, *tmp_iter_page;
  1071. struct buffer_page *last_page, *first_page;
  1072. unsigned int nr_removed;
  1073. unsigned long head_bit;
  1074. int page_entries;
  1075. head_bit = 0;
  1076. raw_spin_lock_irq(&cpu_buffer->reader_lock);
  1077. atomic_inc(&cpu_buffer->record_disabled);
  1078. /*
  1079. * We don't race with the readers since we have acquired the reader
  1080. * lock. We also don't race with writers after disabling recording.
  1081. * This makes it easy to figure out the first and the last page to be
  1082. * removed from the list. We unlink all the pages in between including
  1083. * the first and last pages. This is done in a busy loop so that we
  1084. * lose the least number of traces.
  1085. * The pages are freed after we restart recording and unlock readers.
  1086. */
  1087. tail_page = &cpu_buffer->tail_page->list;
  1088. /*
  1089. * tail page might be on reader page, we remove the next page
  1090. * from the ring buffer
  1091. */
  1092. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  1093. tail_page = rb_list_head(tail_page->next);
  1094. to_remove = tail_page;
  1095. /* start of pages to remove */
  1096. first_page = list_entry(rb_list_head(to_remove->next),
  1097. struct buffer_page, list);
  1098. for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
  1099. to_remove = rb_list_head(to_remove)->next;
  1100. head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
  1101. }
  1102. next_page = rb_list_head(to_remove)->next;
  1103. /*
  1104. * Now we remove all pages between tail_page and next_page.
  1105. * Make sure that we have head_bit value preserved for the
  1106. * next page
  1107. */
  1108. tail_page->next = (struct list_head *)((unsigned long)next_page |
  1109. head_bit);
  1110. next_page = rb_list_head(next_page);
  1111. next_page->prev = tail_page;
  1112. /* make sure pages points to a valid page in the ring buffer */
  1113. cpu_buffer->pages = next_page;
  1114. /* update head page */
  1115. if (head_bit)
  1116. cpu_buffer->head_page = list_entry(next_page,
  1117. struct buffer_page, list);
  1118. /*
  1119. * change read pointer to make sure any read iterators reset
  1120. * themselves
  1121. */
  1122. cpu_buffer->read = 0;
  1123. /* pages are removed, resume tracing and then free the pages */
  1124. atomic_dec(&cpu_buffer->record_disabled);
  1125. raw_spin_unlock_irq(&cpu_buffer->reader_lock);
  1126. RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
  1127. /* last buffer page to remove */
  1128. last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
  1129. list);
  1130. tmp_iter_page = first_page;
  1131. do {
  1132. to_remove_page = tmp_iter_page;
  1133. rb_inc_page(cpu_buffer, &tmp_iter_page);
  1134. /* update the counters */
  1135. page_entries = rb_page_entries(to_remove_page);
  1136. if (page_entries) {
  1137. /*
  1138. * If something was added to this page, it was full
  1139. * since it is not the tail page. So we deduct the
  1140. * bytes consumed in ring buffer from here.
  1141. * No need to update overruns, since this page is
  1142. * deleted from ring buffer and its entries are
  1143. * already accounted for.
  1144. */
  1145. local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
  1146. }
  1147. /*
  1148. * We have already removed references to this list item, just
  1149. * free up the buffer_page and its page
  1150. */
  1151. free_buffer_page(to_remove_page);
  1152. nr_removed--;
  1153. } while (to_remove_page != last_page);
  1154. RB_WARN_ON(cpu_buffer, nr_removed);
  1155. return nr_removed == 0;
  1156. }
  1157. static int
  1158. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
  1159. {
  1160. struct list_head *pages = &cpu_buffer->new_pages;
  1161. int retries, success;
  1162. raw_spin_lock_irq(&cpu_buffer->reader_lock);
  1163. /*
  1164. * We are holding the reader lock, so the reader page won't be swapped
  1165. * in the ring buffer. Now we are racing with the writer trying to
  1166. * move head page and the tail page.
  1167. * We are going to adapt the reader page update process where:
  1168. * 1. We first splice the start and end of list of new pages between
  1169. * the head page and its previous page.
  1170. * 2. We cmpxchg the prev_page->next to point from head page to the
  1171. * start of new pages list.
  1172. * 3. Finally, we update the head->prev to the end of new list.
  1173. *
  1174. * We will try this process 10 times, to make sure that we don't keep
  1175. * spinning.
  1176. */
  1177. retries = 10;
  1178. success = 0;
  1179. while (retries--) {
  1180. struct list_head *head_page, *prev_page, *r;
  1181. struct list_head *last_page, *first_page;
  1182. struct list_head *head_page_with_bit;
  1183. head_page = &rb_set_head_page(cpu_buffer)->list;
  1184. prev_page = head_page->prev;
  1185. first_page = pages->next;
  1186. last_page = pages->prev;
  1187. head_page_with_bit = (struct list_head *)
  1188. ((unsigned long)head_page | RB_PAGE_HEAD);
  1189. last_page->next = head_page_with_bit;
  1190. first_page->prev = prev_page;
  1191. r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
  1192. if (r == head_page_with_bit) {
  1193. /*
  1194. * yay, we replaced the page pointer to our new list,
  1195. * now, we just have to update to head page's prev
  1196. * pointer to point to end of list
  1197. */
  1198. head_page->prev = last_page;
  1199. success = 1;
  1200. break;
  1201. }
  1202. }
  1203. if (success)
  1204. INIT_LIST_HEAD(pages);
  1205. /*
  1206. * If we weren't successful in adding in new pages, warn and stop
  1207. * tracing
  1208. */
  1209. RB_WARN_ON(cpu_buffer, !success);
  1210. raw_spin_unlock_irq(&cpu_buffer->reader_lock);
  1211. /* free pages if they weren't inserted */
  1212. if (!success) {
  1213. struct buffer_page *bpage, *tmp;
  1214. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  1215. list) {
  1216. list_del_init(&bpage->list);
  1217. free_buffer_page(bpage);
  1218. }
  1219. }
  1220. return success;
  1221. }
  1222. static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
  1223. {
  1224. int success;
  1225. if (cpu_buffer->nr_pages_to_update > 0)
  1226. success = rb_insert_pages(cpu_buffer);
  1227. else
  1228. success = rb_remove_pages(cpu_buffer,
  1229. -cpu_buffer->nr_pages_to_update);
  1230. if (success)
  1231. cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
  1232. }
  1233. static void update_pages_handler(struct work_struct *work)
  1234. {
  1235. struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
  1236. struct ring_buffer_per_cpu, update_pages_work);
  1237. rb_update_pages(cpu_buffer);
  1238. complete(&cpu_buffer->update_completion);
  1239. }
  1240. /**
  1241. * ring_buffer_resize - resize the ring buffer
  1242. * @buffer: the buffer to resize.
  1243. * @size: the new size.
  1244. *
  1245. * Minimum size is 2 * BUF_PAGE_SIZE.
  1246. *
  1247. * Returns 0 on success and < 0 on failure.
  1248. */
  1249. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
  1250. int cpu_id)
  1251. {
  1252. struct ring_buffer_per_cpu *cpu_buffer;
  1253. unsigned nr_pages;
  1254. int cpu, err = 0;
  1255. /*
  1256. * Always succeed at resizing a non-existent buffer:
  1257. */
  1258. if (!buffer)
  1259. return size;
  1260. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1261. size *= BUF_PAGE_SIZE;
  1262. /* we need a minimum of two pages */
  1263. if (size < BUF_PAGE_SIZE * 2)
  1264. size = BUF_PAGE_SIZE * 2;
  1265. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1266. /*
  1267. * Don't succeed if resizing is disabled, as a reader might be
  1268. * manipulating the ring buffer and is expecting a sane state while
  1269. * this is true.
  1270. */
  1271. if (atomic_read(&buffer->resize_disabled))
  1272. return -EBUSY;
  1273. /* prevent another thread from changing buffer sizes */
  1274. mutex_lock(&buffer->mutex);
  1275. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  1276. /* calculate the pages to update */
  1277. for_each_buffer_cpu(buffer, cpu) {
  1278. cpu_buffer = buffer->buffers[cpu];
  1279. cpu_buffer->nr_pages_to_update = nr_pages -
  1280. cpu_buffer->nr_pages;
  1281. /*
  1282. * nothing more to do for removing pages or no update
  1283. */
  1284. if (cpu_buffer->nr_pages_to_update <= 0)
  1285. continue;
  1286. /*
  1287. * to add pages, make sure all new pages can be
  1288. * allocated without receiving ENOMEM
  1289. */
  1290. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  1291. if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
  1292. &cpu_buffer->new_pages, cpu)) {
  1293. /* not enough memory for new pages */
  1294. err = -ENOMEM;
  1295. goto out_err;
  1296. }
  1297. }
  1298. get_online_cpus();
  1299. /*
  1300. * Fire off all the required work handlers
  1301. * Look out for offline CPUs
  1302. */
  1303. for_each_buffer_cpu(buffer, cpu) {
  1304. cpu_buffer = buffer->buffers[cpu];
  1305. if (!cpu_buffer->nr_pages_to_update ||
  1306. !cpu_online(cpu))
  1307. continue;
  1308. schedule_work_on(cpu, &cpu_buffer->update_pages_work);
  1309. }
  1310. /*
  1311. * This loop is for the CPUs that are not online.
  1312. * We can't schedule anything on them, but it's not necessary
  1313. * since we can change their buffer sizes without any race.
  1314. */
  1315. for_each_buffer_cpu(buffer, cpu) {
  1316. cpu_buffer = buffer->buffers[cpu];
  1317. if (!cpu_buffer->nr_pages_to_update ||
  1318. cpu_online(cpu))
  1319. continue;
  1320. rb_update_pages(cpu_buffer);
  1321. }
  1322. /* wait for all the updates to complete */
  1323. for_each_buffer_cpu(buffer, cpu) {
  1324. cpu_buffer = buffer->buffers[cpu];
  1325. if (!cpu_buffer->nr_pages_to_update ||
  1326. !cpu_online(cpu))
  1327. continue;
  1328. wait_for_completion(&cpu_buffer->update_completion);
  1329. /* reset this value */
  1330. cpu_buffer->nr_pages_to_update = 0;
  1331. }
  1332. put_online_cpus();
  1333. } else {
  1334. cpu_buffer = buffer->buffers[cpu_id];
  1335. if (nr_pages == cpu_buffer->nr_pages)
  1336. goto out;
  1337. cpu_buffer->nr_pages_to_update = nr_pages -
  1338. cpu_buffer->nr_pages;
  1339. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  1340. if (cpu_buffer->nr_pages_to_update > 0 &&
  1341. __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
  1342. &cpu_buffer->new_pages, cpu_id)) {
  1343. err = -ENOMEM;
  1344. goto out_err;
  1345. }
  1346. get_online_cpus();
  1347. if (cpu_online(cpu_id)) {
  1348. schedule_work_on(cpu_id,
  1349. &cpu_buffer->update_pages_work);
  1350. wait_for_completion(&cpu_buffer->update_completion);
  1351. } else
  1352. rb_update_pages(cpu_buffer);
  1353. put_online_cpus();
  1354. /* reset this value */
  1355. cpu_buffer->nr_pages_to_update = 0;
  1356. }
  1357. out:
  1358. mutex_unlock(&buffer->mutex);
  1359. return size;
  1360. out_err:
  1361. for_each_buffer_cpu(buffer, cpu) {
  1362. struct buffer_page *bpage, *tmp;
  1363. cpu_buffer = buffer->buffers[cpu];
  1364. cpu_buffer->nr_pages_to_update = 0;
  1365. if (list_empty(&cpu_buffer->new_pages))
  1366. continue;
  1367. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  1368. list) {
  1369. list_del_init(&bpage->list);
  1370. free_buffer_page(bpage);
  1371. }
  1372. }
  1373. mutex_unlock(&buffer->mutex);
  1374. return err;
  1375. }
  1376. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  1377. void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
  1378. {
  1379. mutex_lock(&buffer->mutex);
  1380. if (val)
  1381. buffer->flags |= RB_FL_OVERWRITE;
  1382. else
  1383. buffer->flags &= ~RB_FL_OVERWRITE;
  1384. mutex_unlock(&buffer->mutex);
  1385. }
  1386. EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
  1387. static inline void *
  1388. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  1389. {
  1390. return bpage->data + index;
  1391. }
  1392. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  1393. {
  1394. return bpage->page->data + index;
  1395. }
  1396. static inline struct ring_buffer_event *
  1397. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  1398. {
  1399. return __rb_page_index(cpu_buffer->reader_page,
  1400. cpu_buffer->reader_page->read);
  1401. }
  1402. static inline struct ring_buffer_event *
  1403. rb_iter_head_event(struct ring_buffer_iter *iter)
  1404. {
  1405. return __rb_page_index(iter->head_page, iter->head);
  1406. }
  1407. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  1408. {
  1409. return local_read(&bpage->page->commit);
  1410. }
  1411. /* Size is determined by what has been committed */
  1412. static inline unsigned rb_page_size(struct buffer_page *bpage)
  1413. {
  1414. return rb_page_commit(bpage);
  1415. }
  1416. static inline unsigned
  1417. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  1418. {
  1419. return rb_page_commit(cpu_buffer->commit_page);
  1420. }
  1421. static inline unsigned
  1422. rb_event_index(struct ring_buffer_event *event)
  1423. {
  1424. unsigned long addr = (unsigned long)event;
  1425. return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
  1426. }
  1427. static inline int
  1428. rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1429. struct ring_buffer_event *event)
  1430. {
  1431. unsigned long addr = (unsigned long)event;
  1432. unsigned long index;
  1433. index = rb_event_index(event);
  1434. addr &= PAGE_MASK;
  1435. return cpu_buffer->commit_page->page == (void *)addr &&
  1436. rb_commit_index(cpu_buffer) == index;
  1437. }
  1438. static void
  1439. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  1440. {
  1441. unsigned long max_count;
  1442. /*
  1443. * We only race with interrupts and NMIs on this CPU.
  1444. * If we own the commit event, then we can commit
  1445. * all others that interrupted us, since the interruptions
  1446. * are in stack format (they finish before they come
  1447. * back to us). This allows us to do a simple loop to
  1448. * assign the commit to the tail.
  1449. */
  1450. again:
  1451. max_count = cpu_buffer->nr_pages * 100;
  1452. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  1453. if (RB_WARN_ON(cpu_buffer, !(--max_count)))
  1454. return;
  1455. if (RB_WARN_ON(cpu_buffer,
  1456. rb_is_reader_page(cpu_buffer->tail_page)))
  1457. return;
  1458. local_set(&cpu_buffer->commit_page->page->commit,
  1459. rb_page_write(cpu_buffer->commit_page));
  1460. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  1461. cpu_buffer->write_stamp =
  1462. cpu_buffer->commit_page->page->time_stamp;
  1463. /* add barrier to keep gcc from optimizing too much */
  1464. barrier();
  1465. }
  1466. while (rb_commit_index(cpu_buffer) !=
  1467. rb_page_write(cpu_buffer->commit_page)) {
  1468. local_set(&cpu_buffer->commit_page->page->commit,
  1469. rb_page_write(cpu_buffer->commit_page));
  1470. RB_WARN_ON(cpu_buffer,
  1471. local_read(&cpu_buffer->commit_page->page->commit) &
  1472. ~RB_WRITE_MASK);
  1473. barrier();
  1474. }
  1475. /* again, keep gcc from optimizing */
  1476. barrier();
  1477. /*
  1478. * If an interrupt came in just after the first while loop
  1479. * and pushed the tail page forward, we will be left with
  1480. * a dangling commit that will never go forward.
  1481. */
  1482. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  1483. goto again;
  1484. }
  1485. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1486. {
  1487. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  1488. cpu_buffer->reader_page->read = 0;
  1489. }
  1490. static void rb_inc_iter(struct ring_buffer_iter *iter)
  1491. {
  1492. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1493. /*
  1494. * The iterator could be on the reader page (it starts there).
  1495. * But the head could have moved, since the reader was
  1496. * found. Check for this case and assign the iterator
  1497. * to the head page instead of next.
  1498. */
  1499. if (iter->head_page == cpu_buffer->reader_page)
  1500. iter->head_page = rb_set_head_page(cpu_buffer);
  1501. else
  1502. rb_inc_page(cpu_buffer, &iter->head_page);
  1503. iter->read_stamp = iter->head_page->page->time_stamp;
  1504. iter->head = 0;
  1505. }
  1506. /* Slow path, do not inline */
  1507. static noinline struct ring_buffer_event *
  1508. rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
  1509. {
  1510. event->type_len = RINGBUF_TYPE_TIME_EXTEND;
  1511. /* Not the first event on the page? */
  1512. if (rb_event_index(event)) {
  1513. event->time_delta = delta & TS_MASK;
  1514. event->array[0] = delta >> TS_SHIFT;
  1515. } else {
  1516. /* nope, just zero it */
  1517. event->time_delta = 0;
  1518. event->array[0] = 0;
  1519. }
  1520. return skip_time_extend(event);
  1521. }
  1522. /**
  1523. * ring_buffer_update_event - update event type and data
  1524. * @event: the even to update
  1525. * @type: the type of event
  1526. * @length: the size of the event field in the ring buffer
  1527. *
  1528. * Update the type and data fields of the event. The length
  1529. * is the actual size that is written to the ring buffer,
  1530. * and with this, we can determine what to place into the
  1531. * data field.
  1532. */
  1533. static void
  1534. rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
  1535. struct ring_buffer_event *event, unsigned length,
  1536. int add_timestamp, u64 delta)
  1537. {
  1538. /* Only a commit updates the timestamp */
  1539. if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
  1540. delta = 0;
  1541. /*
  1542. * If we need to add a timestamp, then we
  1543. * add it to the start of the resevered space.
  1544. */
  1545. if (unlikely(add_timestamp)) {
  1546. event = rb_add_time_stamp(event, delta);
  1547. length -= RB_LEN_TIME_EXTEND;
  1548. delta = 0;
  1549. }
  1550. event->time_delta = delta;
  1551. length -= RB_EVNT_HDR_SIZE;
  1552. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
  1553. event->type_len = 0;
  1554. event->array[0] = length;
  1555. } else
  1556. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  1557. }
  1558. /*
  1559. * rb_handle_head_page - writer hit the head page
  1560. *
  1561. * Returns: +1 to retry page
  1562. * 0 to continue
  1563. * -1 on error
  1564. */
  1565. static int
  1566. rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  1567. struct buffer_page *tail_page,
  1568. struct buffer_page *next_page)
  1569. {
  1570. struct buffer_page *new_head;
  1571. int entries;
  1572. int type;
  1573. int ret;
  1574. entries = rb_page_entries(next_page);
  1575. /*
  1576. * The hard part is here. We need to move the head
  1577. * forward, and protect against both readers on
  1578. * other CPUs and writers coming in via interrupts.
  1579. */
  1580. type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
  1581. RB_PAGE_HEAD);
  1582. /*
  1583. * type can be one of four:
  1584. * NORMAL - an interrupt already moved it for us
  1585. * HEAD - we are the first to get here.
  1586. * UPDATE - we are the interrupt interrupting
  1587. * a current move.
  1588. * MOVED - a reader on another CPU moved the next
  1589. * pointer to its reader page. Give up
  1590. * and try again.
  1591. */
  1592. switch (type) {
  1593. case RB_PAGE_HEAD:
  1594. /*
  1595. * We changed the head to UPDATE, thus
  1596. * it is our responsibility to update
  1597. * the counters.
  1598. */
  1599. local_add(entries, &cpu_buffer->overrun);
  1600. local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
  1601. /*
  1602. * The entries will be zeroed out when we move the
  1603. * tail page.
  1604. */
  1605. /* still more to do */
  1606. break;
  1607. case RB_PAGE_UPDATE:
  1608. /*
  1609. * This is an interrupt that interrupt the
  1610. * previous update. Still more to do.
  1611. */
  1612. break;
  1613. case RB_PAGE_NORMAL:
  1614. /*
  1615. * An interrupt came in before the update
  1616. * and processed this for us.
  1617. * Nothing left to do.
  1618. */
  1619. return 1;
  1620. case RB_PAGE_MOVED:
  1621. /*
  1622. * The reader is on another CPU and just did
  1623. * a swap with our next_page.
  1624. * Try again.
  1625. */
  1626. return 1;
  1627. default:
  1628. RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
  1629. return -1;
  1630. }
  1631. /*
  1632. * Now that we are here, the old head pointer is
  1633. * set to UPDATE. This will keep the reader from
  1634. * swapping the head page with the reader page.
  1635. * The reader (on another CPU) will spin till
  1636. * we are finished.
  1637. *
  1638. * We just need to protect against interrupts
  1639. * doing the job. We will set the next pointer
  1640. * to HEAD. After that, we set the old pointer
  1641. * to NORMAL, but only if it was HEAD before.
  1642. * otherwise we are an interrupt, and only
  1643. * want the outer most commit to reset it.
  1644. */
  1645. new_head = next_page;
  1646. rb_inc_page(cpu_buffer, &new_head);
  1647. ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
  1648. RB_PAGE_NORMAL);
  1649. /*
  1650. * Valid returns are:
  1651. * HEAD - an interrupt came in and already set it.
  1652. * NORMAL - One of two things:
  1653. * 1) We really set it.
  1654. * 2) A bunch of interrupts came in and moved
  1655. * the page forward again.
  1656. */
  1657. switch (ret) {
  1658. case RB_PAGE_HEAD:
  1659. case RB_PAGE_NORMAL:
  1660. /* OK */
  1661. break;
  1662. default:
  1663. RB_WARN_ON(cpu_buffer, 1);
  1664. return -1;
  1665. }
  1666. /*
  1667. * It is possible that an interrupt came in,
  1668. * set the head up, then more interrupts came in
  1669. * and moved it again. When we get back here,
  1670. * the page would have been set to NORMAL but we
  1671. * just set it back to HEAD.
  1672. *
  1673. * How do you detect this? Well, if that happened
  1674. * the tail page would have moved.
  1675. */
  1676. if (ret == RB_PAGE_NORMAL) {
  1677. /*
  1678. * If the tail had moved passed next, then we need
  1679. * to reset the pointer.
  1680. */
  1681. if (cpu_buffer->tail_page != tail_page &&
  1682. cpu_buffer->tail_page != next_page)
  1683. rb_head_page_set_normal(cpu_buffer, new_head,
  1684. next_page,
  1685. RB_PAGE_HEAD);
  1686. }
  1687. /*
  1688. * If this was the outer most commit (the one that
  1689. * changed the original pointer from HEAD to UPDATE),
  1690. * then it is up to us to reset it to NORMAL.
  1691. */
  1692. if (type == RB_PAGE_HEAD) {
  1693. ret = rb_head_page_set_normal(cpu_buffer, next_page,
  1694. tail_page,
  1695. RB_PAGE_UPDATE);
  1696. if (RB_WARN_ON(cpu_buffer,
  1697. ret != RB_PAGE_UPDATE))
  1698. return -1;
  1699. }
  1700. return 0;
  1701. }
  1702. static unsigned rb_calculate_event_length(unsigned length)
  1703. {
  1704. struct ring_buffer_event event; /* Used only for sizeof array */
  1705. /* zero length can cause confusions */
  1706. if (!length)
  1707. length = 1;
  1708. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
  1709. length += sizeof(event.array[0]);
  1710. length += RB_EVNT_HDR_SIZE;
  1711. length = ALIGN(length, RB_ARCH_ALIGNMENT);
  1712. return length;
  1713. }
  1714. static inline void
  1715. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1716. struct buffer_page *tail_page,
  1717. unsigned long tail, unsigned long length)
  1718. {
  1719. struct ring_buffer_event *event;
  1720. /*
  1721. * Only the event that crossed the page boundary
  1722. * must fill the old tail_page with padding.
  1723. */
  1724. if (tail >= BUF_PAGE_SIZE) {
  1725. /*
  1726. * If the page was filled, then we still need
  1727. * to update the real_end. Reset it to zero
  1728. * and the reader will ignore it.
  1729. */
  1730. if (tail == BUF_PAGE_SIZE)
  1731. tail_page->real_end = 0;
  1732. local_sub(length, &tail_page->write);
  1733. return;
  1734. }
  1735. event = __rb_page_index(tail_page, tail);
  1736. kmemcheck_annotate_bitfield(event, bitfield);
  1737. /* account for padding bytes */
  1738. local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
  1739. /*
  1740. * Save the original length to the meta data.
  1741. * This will be used by the reader to add lost event
  1742. * counter.
  1743. */
  1744. tail_page->real_end = tail;
  1745. /*
  1746. * If this event is bigger than the minimum size, then
  1747. * we need to be careful that we don't subtract the
  1748. * write counter enough to allow another writer to slip
  1749. * in on this page.
  1750. * We put in a discarded commit instead, to make sure
  1751. * that this space is not used again.
  1752. *
  1753. * If we are less than the minimum size, we don't need to
  1754. * worry about it.
  1755. */
  1756. if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
  1757. /* No room for any events */
  1758. /* Mark the rest of the page with padding */
  1759. rb_event_set_padding(event);
  1760. /* Set the write back to the previous setting */
  1761. local_sub(length, &tail_page->write);
  1762. return;
  1763. }
  1764. /* Put in a discarded event */
  1765. event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
  1766. event->type_len = RINGBUF_TYPE_PADDING;
  1767. /* time delta must be non zero */
  1768. event->time_delta = 1;
  1769. /* Set write to end of buffer */
  1770. length = (tail + length) - BUF_PAGE_SIZE;
  1771. local_sub(length, &tail_page->write);
  1772. }
  1773. /*
  1774. * This is the slow path, force gcc not to inline it.
  1775. */
  1776. static noinline struct ring_buffer_event *
  1777. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1778. unsigned long length, unsigned long tail,
  1779. struct buffer_page *tail_page, u64 ts)
  1780. {
  1781. struct buffer_page *commit_page = cpu_buffer->commit_page;
  1782. struct ring_buffer *buffer = cpu_buffer->buffer;
  1783. struct buffer_page *next_page;
  1784. int ret;
  1785. next_page = tail_page;
  1786. rb_inc_page(cpu_buffer, &next_page);
  1787. /*
  1788. * If for some reason, we had an interrupt storm that made
  1789. * it all the way around the buffer, bail, and warn
  1790. * about it.
  1791. */
  1792. if (unlikely(next_page == commit_page)) {
  1793. local_inc(&cpu_buffer->commit_overrun);
  1794. goto out_reset;
  1795. }
  1796. /*
  1797. * This is where the fun begins!
  1798. *
  1799. * We are fighting against races between a reader that
  1800. * could be on another CPU trying to swap its reader
  1801. * page with the buffer head.
  1802. *
  1803. * We are also fighting against interrupts coming in and
  1804. * moving the head or tail on us as well.
  1805. *
  1806. * If the next page is the head page then we have filled
  1807. * the buffer, unless the commit page is still on the
  1808. * reader page.
  1809. */
  1810. if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
  1811. /*
  1812. * If the commit is not on the reader page, then
  1813. * move the header page.
  1814. */
  1815. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  1816. /*
  1817. * If we are not in overwrite mode,
  1818. * this is easy, just stop here.
  1819. */
  1820. if (!(buffer->flags & RB_FL_OVERWRITE))
  1821. goto out_reset;
  1822. ret = rb_handle_head_page(cpu_buffer,
  1823. tail_page,
  1824. next_page);
  1825. if (ret < 0)
  1826. goto out_reset;
  1827. if (ret)
  1828. goto out_again;
  1829. } else {
  1830. /*
  1831. * We need to be careful here too. The
  1832. * commit page could still be on the reader
  1833. * page. We could have a small buffer, and
  1834. * have filled up the buffer with events
  1835. * from interrupts and such, and wrapped.
  1836. *
  1837. * Note, if the tail page is also the on the
  1838. * reader_page, we let it move out.
  1839. */
  1840. if (unlikely((cpu_buffer->commit_page !=
  1841. cpu_buffer->tail_page) &&
  1842. (cpu_buffer->commit_page ==
  1843. cpu_buffer->reader_page))) {
  1844. local_inc(&cpu_buffer->commit_overrun);
  1845. goto out_reset;
  1846. }
  1847. }
  1848. }
  1849. ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
  1850. if (ret) {
  1851. /*
  1852. * Nested commits always have zero deltas, so
  1853. * just reread the time stamp
  1854. */
  1855. ts = rb_time_stamp(buffer);
  1856. next_page->page->time_stamp = ts;
  1857. }
  1858. out_again:
  1859. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1860. /* fail and let the caller try again */
  1861. return ERR_PTR(-EAGAIN);
  1862. out_reset:
  1863. /* reset write */
  1864. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1865. return NULL;
  1866. }
  1867. static struct ring_buffer_event *
  1868. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  1869. unsigned long length, u64 ts,
  1870. u64 delta, int add_timestamp)
  1871. {
  1872. struct buffer_page *tail_page;
  1873. struct ring_buffer_event *event;
  1874. unsigned long tail, write;
  1875. /*
  1876. * If the time delta since the last event is too big to
  1877. * hold in the time field of the event, then we append a
  1878. * TIME EXTEND event ahead of the data event.
  1879. */
  1880. if (unlikely(add_timestamp))
  1881. length += RB_LEN_TIME_EXTEND;
  1882. tail_page = cpu_buffer->tail_page;
  1883. write = local_add_return(length, &tail_page->write);
  1884. /* set write to only the index of the write */
  1885. write &= RB_WRITE_MASK;
  1886. tail = write - length;
  1887. /* See if we shot pass the end of this buffer page */
  1888. if (unlikely(write > BUF_PAGE_SIZE))
  1889. return rb_move_tail(cpu_buffer, length, tail,
  1890. tail_page, ts);
  1891. /* We reserved something on the buffer */
  1892. event = __rb_page_index(tail_page, tail);
  1893. kmemcheck_annotate_bitfield(event, bitfield);
  1894. rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
  1895. local_inc(&tail_page->entries);
  1896. /*
  1897. * If this is the first commit on the page, then update
  1898. * its timestamp.
  1899. */
  1900. if (!tail)
  1901. tail_page->page->time_stamp = ts;
  1902. /* account for these added bytes */
  1903. local_add(length, &cpu_buffer->entries_bytes);
  1904. return event;
  1905. }
  1906. static inline int
  1907. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  1908. struct ring_buffer_event *event)
  1909. {
  1910. unsigned long new_index, old_index;
  1911. struct buffer_page *bpage;
  1912. unsigned long index;
  1913. unsigned long addr;
  1914. new_index = rb_event_index(event);
  1915. old_index = new_index + rb_event_ts_length(event);
  1916. addr = (unsigned long)event;
  1917. addr &= PAGE_MASK;
  1918. bpage = cpu_buffer->tail_page;
  1919. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  1920. unsigned long write_mask =
  1921. local_read(&bpage->write) & ~RB_WRITE_MASK;
  1922. unsigned long event_length = rb_event_length(event);
  1923. /*
  1924. * This is on the tail page. It is possible that
  1925. * a write could come in and move the tail page
  1926. * and write to the next page. That is fine
  1927. * because we just shorten what is on this page.
  1928. */
  1929. old_index += write_mask;
  1930. new_index += write_mask;
  1931. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1932. if (index == old_index) {
  1933. /* update counters */
  1934. local_sub(event_length, &cpu_buffer->entries_bytes);
  1935. return 1;
  1936. }
  1937. }
  1938. /* could not discard */
  1939. return 0;
  1940. }
  1941. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1942. {
  1943. local_inc(&cpu_buffer->committing);
  1944. local_inc(&cpu_buffer->commits);
  1945. }
  1946. static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1947. {
  1948. unsigned long commits;
  1949. if (RB_WARN_ON(cpu_buffer,
  1950. !local_read(&cpu_buffer->committing)))
  1951. return;
  1952. again:
  1953. commits = local_read(&cpu_buffer->commits);
  1954. /* synchronize with interrupts */
  1955. barrier();
  1956. if (local_read(&cpu_buffer->committing) == 1)
  1957. rb_set_commit_to_write(cpu_buffer);
  1958. local_dec(&cpu_buffer->committing);
  1959. /* synchronize with interrupts */
  1960. barrier();
  1961. /*
  1962. * Need to account for interrupts coming in between the
  1963. * updating of the commit page and the clearing of the
  1964. * committing counter.
  1965. */
  1966. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  1967. !local_read(&cpu_buffer->committing)) {
  1968. local_inc(&cpu_buffer->committing);
  1969. goto again;
  1970. }
  1971. }
  1972. static struct ring_buffer_event *
  1973. rb_reserve_next_event(struct ring_buffer *buffer,
  1974. struct ring_buffer_per_cpu *cpu_buffer,
  1975. unsigned long length)
  1976. {
  1977. struct ring_buffer_event *event;
  1978. u64 ts, delta;
  1979. int nr_loops = 0;
  1980. int add_timestamp;
  1981. u64 diff;
  1982. rb_start_commit(cpu_buffer);
  1983. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  1984. /*
  1985. * Due to the ability to swap a cpu buffer from a buffer
  1986. * it is possible it was swapped before we committed.
  1987. * (committing stops a swap). We check for it here and
  1988. * if it happened, we have to fail the write.
  1989. */
  1990. barrier();
  1991. if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
  1992. local_dec(&cpu_buffer->committing);
  1993. local_dec(&cpu_buffer->commits);
  1994. return NULL;
  1995. }
  1996. #endif
  1997. length = rb_calculate_event_length(length);
  1998. again:
  1999. add_timestamp = 0;
  2000. delta = 0;
  2001. /*
  2002. * We allow for interrupts to reenter here and do a trace.
  2003. * If one does, it will cause this original code to loop
  2004. * back here. Even with heavy interrupts happening, this
  2005. * should only happen a few times in a row. If this happens
  2006. * 1000 times in a row, there must be either an interrupt
  2007. * storm or we have something buggy.
  2008. * Bail!
  2009. */
  2010. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  2011. goto out_fail;
  2012. ts = rb_time_stamp(cpu_buffer->buffer);
  2013. diff = ts - cpu_buffer->write_stamp;
  2014. /* make sure this diff is calculated here */
  2015. barrier();
  2016. /* Did the write stamp get updated already? */
  2017. if (likely(ts >= cpu_buffer->write_stamp)) {
  2018. delta = diff;
  2019. if (unlikely(test_time_stamp(delta))) {
  2020. int local_clock_stable = 1;
  2021. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  2022. local_clock_stable = sched_clock_stable;
  2023. #endif
  2024. WARN_ONCE(delta > (1ULL << 59),
  2025. KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
  2026. (unsigned long long)delta,
  2027. (unsigned long long)ts,
  2028. (unsigned long long)cpu_buffer->write_stamp,
  2029. local_clock_stable ? "" :
  2030. "If you just came from a suspend/resume,\n"
  2031. "please switch to the trace global clock:\n"
  2032. " echo global > /sys/kernel/debug/tracing/trace_clock\n");
  2033. add_timestamp = 1;
  2034. }
  2035. }
  2036. event = __rb_reserve_next(cpu_buffer, length, ts,
  2037. delta, add_timestamp);
  2038. if (unlikely(PTR_ERR(event) == -EAGAIN))
  2039. goto again;
  2040. if (!event)
  2041. goto out_fail;
  2042. return event;
  2043. out_fail:
  2044. rb_end_commit(cpu_buffer);
  2045. return NULL;
  2046. }
  2047. #ifdef CONFIG_TRACING
  2048. #define TRACE_RECURSIVE_DEPTH 16
  2049. /* Keep this code out of the fast path cache */
  2050. static noinline void trace_recursive_fail(void)
  2051. {
  2052. /* Disable all tracing before we do anything else */
  2053. tracing_off_permanent();
  2054. printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
  2055. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  2056. trace_recursion_buffer(),
  2057. hardirq_count() >> HARDIRQ_SHIFT,
  2058. softirq_count() >> SOFTIRQ_SHIFT,
  2059. in_nmi());
  2060. WARN_ON_ONCE(1);
  2061. }
  2062. static inline int trace_recursive_lock(void)
  2063. {
  2064. trace_recursion_inc();
  2065. if (likely(trace_recursion_buffer() < TRACE_RECURSIVE_DEPTH))
  2066. return 0;
  2067. trace_recursive_fail();
  2068. return -1;
  2069. }
  2070. static inline void trace_recursive_unlock(void)
  2071. {
  2072. WARN_ON_ONCE(!trace_recursion_buffer());
  2073. trace_recursion_dec();
  2074. }
  2075. #else
  2076. #define trace_recursive_lock() (0)
  2077. #define trace_recursive_unlock() do { } while (0)
  2078. #endif
  2079. /**
  2080. * ring_buffer_lock_reserve - reserve a part of the buffer
  2081. * @buffer: the ring buffer to reserve from
  2082. * @length: the length of the data to reserve (excluding event header)
  2083. *
  2084. * Returns a reseverd event on the ring buffer to copy directly to.
  2085. * The user of this interface will need to get the body to write into
  2086. * and can use the ring_buffer_event_data() interface.
  2087. *
  2088. * The length is the length of the data needed, not the event length
  2089. * which also includes the event header.
  2090. *
  2091. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  2092. * If NULL is returned, then nothing has been allocated or locked.
  2093. */
  2094. struct ring_buffer_event *
  2095. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  2096. {
  2097. struct ring_buffer_per_cpu *cpu_buffer;
  2098. struct ring_buffer_event *event;
  2099. int cpu;
  2100. if (ring_buffer_flags != RB_BUFFERS_ON)
  2101. return NULL;
  2102. /* If we are tracing schedule, we don't want to recurse */
  2103. preempt_disable_notrace();
  2104. if (atomic_read(&buffer->record_disabled))
  2105. goto out_nocheck;
  2106. if (trace_recursive_lock())
  2107. goto out_nocheck;
  2108. cpu = raw_smp_processor_id();
  2109. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2110. goto out;
  2111. cpu_buffer = buffer->buffers[cpu];
  2112. if (atomic_read(&cpu_buffer->record_disabled))
  2113. goto out;
  2114. if (length > BUF_MAX_DATA_SIZE)
  2115. goto out;
  2116. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  2117. if (!event)
  2118. goto out;
  2119. return event;
  2120. out:
  2121. trace_recursive_unlock();
  2122. out_nocheck:
  2123. preempt_enable_notrace();
  2124. return NULL;
  2125. }
  2126. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  2127. static void
  2128. rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2129. struct ring_buffer_event *event)
  2130. {
  2131. u64 delta;
  2132. /*
  2133. * The event first in the commit queue updates the
  2134. * time stamp.
  2135. */
  2136. if (rb_event_is_commit(cpu_buffer, event)) {
  2137. /*
  2138. * A commit event that is first on a page
  2139. * updates the write timestamp with the page stamp
  2140. */
  2141. if (!rb_event_index(event))
  2142. cpu_buffer->write_stamp =
  2143. cpu_buffer->commit_page->page->time_stamp;
  2144. else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
  2145. delta = event->array[0];
  2146. delta <<= TS_SHIFT;
  2147. delta += event->time_delta;
  2148. cpu_buffer->write_stamp += delta;
  2149. } else
  2150. cpu_buffer->write_stamp += event->time_delta;
  2151. }
  2152. }
  2153. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  2154. struct ring_buffer_event *event)
  2155. {
  2156. local_inc(&cpu_buffer->entries);
  2157. rb_update_write_stamp(cpu_buffer, event);
  2158. rb_end_commit(cpu_buffer);
  2159. }
  2160. /**
  2161. * ring_buffer_unlock_commit - commit a reserved
  2162. * @buffer: The buffer to commit to
  2163. * @event: The event pointer to commit.
  2164. *
  2165. * This commits the data to the ring buffer, and releases any locks held.
  2166. *
  2167. * Must be paired with ring_buffer_lock_reserve.
  2168. */
  2169. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  2170. struct ring_buffer_event *event)
  2171. {
  2172. struct ring_buffer_per_cpu *cpu_buffer;
  2173. int cpu = raw_smp_processor_id();
  2174. cpu_buffer = buffer->buffers[cpu];
  2175. rb_commit(cpu_buffer, event);
  2176. trace_recursive_unlock();
  2177. preempt_enable_notrace();
  2178. return 0;
  2179. }
  2180. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  2181. static inline void rb_event_discard(struct ring_buffer_event *event)
  2182. {
  2183. if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
  2184. event = skip_time_extend(event);
  2185. /* array[0] holds the actual length for the discarded event */
  2186. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  2187. event->type_len = RINGBUF_TYPE_PADDING;
  2188. /* time delta must be non zero */
  2189. if (!event->time_delta)
  2190. event->time_delta = 1;
  2191. }
  2192. /*
  2193. * Decrement the entries to the page that an event is on.
  2194. * The event does not even need to exist, only the pointer
  2195. * to the page it is on. This may only be called before the commit
  2196. * takes place.
  2197. */
  2198. static inline void
  2199. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  2200. struct ring_buffer_event *event)
  2201. {
  2202. unsigned long addr = (unsigned long)event;
  2203. struct buffer_page *bpage = cpu_buffer->commit_page;
  2204. struct buffer_page *start;
  2205. addr &= PAGE_MASK;
  2206. /* Do the likely case first */
  2207. if (likely(bpage->page == (void *)addr)) {
  2208. local_dec(&bpage->entries);
  2209. return;
  2210. }
  2211. /*
  2212. * Because the commit page may be on the reader page we
  2213. * start with the next page and check the end loop there.
  2214. */
  2215. rb_inc_page(cpu_buffer, &bpage);
  2216. start = bpage;
  2217. do {
  2218. if (bpage->page == (void *)addr) {
  2219. local_dec(&bpage->entries);
  2220. return;
  2221. }
  2222. rb_inc_page(cpu_buffer, &bpage);
  2223. } while (bpage != start);
  2224. /* commit not part of this buffer?? */
  2225. RB_WARN_ON(cpu_buffer, 1);
  2226. }
  2227. /**
  2228. * ring_buffer_commit_discard - discard an event that has not been committed
  2229. * @buffer: the ring buffer
  2230. * @event: non committed event to discard
  2231. *
  2232. * Sometimes an event that is in the ring buffer needs to be ignored.
  2233. * This function lets the user discard an event in the ring buffer
  2234. * and then that event will not be read later.
  2235. *
  2236. * This function only works if it is called before the the item has been
  2237. * committed. It will try to free the event from the ring buffer
  2238. * if another event has not been added behind it.
  2239. *
  2240. * If another event has been added behind it, it will set the event
  2241. * up as discarded, and perform the commit.
  2242. *
  2243. * If this function is called, do not call ring_buffer_unlock_commit on
  2244. * the event.
  2245. */
  2246. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  2247. struct ring_buffer_event *event)
  2248. {
  2249. struct ring_buffer_per_cpu *cpu_buffer;
  2250. int cpu;
  2251. /* The event is discarded regardless */
  2252. rb_event_discard(event);
  2253. cpu = smp_processor_id();
  2254. cpu_buffer = buffer->buffers[cpu];
  2255. /*
  2256. * This must only be called if the event has not been
  2257. * committed yet. Thus we can assume that preemption
  2258. * is still disabled.
  2259. */
  2260. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  2261. rb_decrement_entry(cpu_buffer, event);
  2262. if (rb_try_to_discard(cpu_buffer, event))
  2263. goto out;
  2264. /*
  2265. * The commit is still visible by the reader, so we
  2266. * must still update the timestamp.
  2267. */
  2268. rb_update_write_stamp(cpu_buffer, event);
  2269. out:
  2270. rb_end_commit(cpu_buffer);
  2271. trace_recursive_unlock();
  2272. preempt_enable_notrace();
  2273. }
  2274. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  2275. /**
  2276. * ring_buffer_write - write data to the buffer without reserving
  2277. * @buffer: The ring buffer to write to.
  2278. * @length: The length of the data being written (excluding the event header)
  2279. * @data: The data to write to the buffer.
  2280. *
  2281. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  2282. * one function. If you already have the data to write to the buffer, it
  2283. * may be easier to simply call this function.
  2284. *
  2285. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  2286. * and not the length of the event which would hold the header.
  2287. */
  2288. int ring_buffer_write(struct ring_buffer *buffer,
  2289. unsigned long length,
  2290. void *data)
  2291. {
  2292. struct ring_buffer_per_cpu *cpu_buffer;
  2293. struct ring_buffer_event *event;
  2294. void *body;
  2295. int ret = -EBUSY;
  2296. int cpu;
  2297. if (ring_buffer_flags != RB_BUFFERS_ON)
  2298. return -EBUSY;
  2299. preempt_disable_notrace();
  2300. if (atomic_read(&buffer->record_disabled))
  2301. goto out;
  2302. cpu = raw_smp_processor_id();
  2303. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2304. goto out;
  2305. cpu_buffer = buffer->buffers[cpu];
  2306. if (atomic_read(&cpu_buffer->record_disabled))
  2307. goto out;
  2308. if (length > BUF_MAX_DATA_SIZE)
  2309. goto out;
  2310. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  2311. if (!event)
  2312. goto out;
  2313. body = rb_event_data(event);
  2314. memcpy(body, data, length);
  2315. rb_commit(cpu_buffer, event);
  2316. ret = 0;
  2317. out:
  2318. preempt_enable_notrace();
  2319. return ret;
  2320. }
  2321. EXPORT_SYMBOL_GPL(ring_buffer_write);
  2322. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  2323. {
  2324. struct buffer_page *reader = cpu_buffer->reader_page;
  2325. struct buffer_page *head = rb_set_head_page(cpu_buffer);
  2326. struct buffer_page *commit = cpu_buffer->commit_page;
  2327. /* In case of error, head will be NULL */
  2328. if (unlikely(!head))
  2329. return 1;
  2330. return reader->read == rb_page_commit(reader) &&
  2331. (commit == reader ||
  2332. (commit == head &&
  2333. head->read == rb_page_commit(commit)));
  2334. }
  2335. /**
  2336. * ring_buffer_record_disable - stop all writes into the buffer
  2337. * @buffer: The ring buffer to stop writes to.
  2338. *
  2339. * This prevents all writes to the buffer. Any attempt to write
  2340. * to the buffer after this will fail and return NULL.
  2341. *
  2342. * The caller should call synchronize_sched() after this.
  2343. */
  2344. void ring_buffer_record_disable(struct ring_buffer *buffer)
  2345. {
  2346. atomic_inc(&buffer->record_disabled);
  2347. }
  2348. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  2349. /**
  2350. * ring_buffer_record_enable - enable writes to the buffer
  2351. * @buffer: The ring buffer to enable writes
  2352. *
  2353. * Note, multiple disables will need the same number of enables
  2354. * to truly enable the writing (much like preempt_disable).
  2355. */
  2356. void ring_buffer_record_enable(struct ring_buffer *buffer)
  2357. {
  2358. atomic_dec(&buffer->record_disabled);
  2359. }
  2360. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  2361. /**
  2362. * ring_buffer_record_off - stop all writes into the buffer
  2363. * @buffer: The ring buffer to stop writes to.
  2364. *
  2365. * This prevents all writes to the buffer. Any attempt to write
  2366. * to the buffer after this will fail and return NULL.
  2367. *
  2368. * This is different than ring_buffer_record_disable() as
  2369. * it works like an on/off switch, where as the disable() verison
  2370. * must be paired with a enable().
  2371. */
  2372. void ring_buffer_record_off(struct ring_buffer *buffer)
  2373. {
  2374. unsigned int rd;
  2375. unsigned int new_rd;
  2376. do {
  2377. rd = atomic_read(&buffer->record_disabled);
  2378. new_rd = rd | RB_BUFFER_OFF;
  2379. } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
  2380. }
  2381. EXPORT_SYMBOL_GPL(ring_buffer_record_off);
  2382. /**
  2383. * ring_buffer_record_on - restart writes into the buffer
  2384. * @buffer: The ring buffer to start writes to.
  2385. *
  2386. * This enables all writes to the buffer that was disabled by
  2387. * ring_buffer_record_off().
  2388. *
  2389. * This is different than ring_buffer_record_enable() as
  2390. * it works like an on/off switch, where as the enable() verison
  2391. * must be paired with a disable().
  2392. */
  2393. void ring_buffer_record_on(struct ring_buffer *buffer)
  2394. {
  2395. unsigned int rd;
  2396. unsigned int new_rd;
  2397. do {
  2398. rd = atomic_read(&buffer->record_disabled);
  2399. new_rd = rd & ~RB_BUFFER_OFF;
  2400. } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
  2401. }
  2402. EXPORT_SYMBOL_GPL(ring_buffer_record_on);
  2403. /**
  2404. * ring_buffer_record_is_on - return true if the ring buffer can write
  2405. * @buffer: The ring buffer to see if write is enabled
  2406. *
  2407. * Returns true if the ring buffer is in a state that it accepts writes.
  2408. */
  2409. int ring_buffer_record_is_on(struct ring_buffer *buffer)
  2410. {
  2411. return !atomic_read(&buffer->record_disabled);
  2412. }
  2413. /**
  2414. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  2415. * @buffer: The ring buffer to stop writes to.
  2416. * @cpu: The CPU buffer to stop
  2417. *
  2418. * This prevents all writes to the buffer. Any attempt to write
  2419. * to the buffer after this will fail and return NULL.
  2420. *
  2421. * The caller should call synchronize_sched() after this.
  2422. */
  2423. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  2424. {
  2425. struct ring_buffer_per_cpu *cpu_buffer;
  2426. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2427. return;
  2428. cpu_buffer = buffer->buffers[cpu];
  2429. atomic_inc(&cpu_buffer->record_disabled);
  2430. }
  2431. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  2432. /**
  2433. * ring_buffer_record_enable_cpu - enable writes to the buffer
  2434. * @buffer: The ring buffer to enable writes
  2435. * @cpu: The CPU to enable.
  2436. *
  2437. * Note, multiple disables will need the same number of enables
  2438. * to truly enable the writing (much like preempt_disable).
  2439. */
  2440. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  2441. {
  2442. struct ring_buffer_per_cpu *cpu_buffer;
  2443. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2444. return;
  2445. cpu_buffer = buffer->buffers[cpu];
  2446. atomic_dec(&cpu_buffer->record_disabled);
  2447. }
  2448. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  2449. /*
  2450. * The total entries in the ring buffer is the running counter
  2451. * of entries entered into the ring buffer, minus the sum of
  2452. * the entries read from the ring buffer and the number of
  2453. * entries that were overwritten.
  2454. */
  2455. static inline unsigned long
  2456. rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
  2457. {
  2458. return local_read(&cpu_buffer->entries) -
  2459. (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
  2460. }
  2461. /**
  2462. * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
  2463. * @buffer: The ring buffer
  2464. * @cpu: The per CPU buffer to read from.
  2465. */
  2466. unsigned long ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
  2467. {
  2468. unsigned long flags;
  2469. struct ring_buffer_per_cpu *cpu_buffer;
  2470. struct buffer_page *bpage;
  2471. unsigned long ret;
  2472. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2473. return 0;
  2474. cpu_buffer = buffer->buffers[cpu];
  2475. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2476. /*
  2477. * if the tail is on reader_page, oldest time stamp is on the reader
  2478. * page
  2479. */
  2480. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  2481. bpage = cpu_buffer->reader_page;
  2482. else
  2483. bpage = rb_set_head_page(cpu_buffer);
  2484. ret = bpage->page->time_stamp;
  2485. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2486. return ret;
  2487. }
  2488. EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
  2489. /**
  2490. * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
  2491. * @buffer: The ring buffer
  2492. * @cpu: The per CPU buffer to read from.
  2493. */
  2494. unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
  2495. {
  2496. struct ring_buffer_per_cpu *cpu_buffer;
  2497. unsigned long ret;
  2498. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2499. return 0;
  2500. cpu_buffer = buffer->buffers[cpu];
  2501. ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
  2502. return ret;
  2503. }
  2504. EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
  2505. /**
  2506. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  2507. * @buffer: The ring buffer
  2508. * @cpu: The per CPU buffer to get the entries from.
  2509. */
  2510. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  2511. {
  2512. struct ring_buffer_per_cpu *cpu_buffer;
  2513. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2514. return 0;
  2515. cpu_buffer = buffer->buffers[cpu];
  2516. return rb_num_of_entries(cpu_buffer);
  2517. }
  2518. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  2519. /**
  2520. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  2521. * @buffer: The ring buffer
  2522. * @cpu: The per CPU buffer to get the number of overruns from
  2523. */
  2524. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2525. {
  2526. struct ring_buffer_per_cpu *cpu_buffer;
  2527. unsigned long ret;
  2528. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2529. return 0;
  2530. cpu_buffer = buffer->buffers[cpu];
  2531. ret = local_read(&cpu_buffer->overrun);
  2532. return ret;
  2533. }
  2534. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  2535. /**
  2536. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  2537. * @buffer: The ring buffer
  2538. * @cpu: The per CPU buffer to get the number of overruns from
  2539. */
  2540. unsigned long
  2541. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2542. {
  2543. struct ring_buffer_per_cpu *cpu_buffer;
  2544. unsigned long ret;
  2545. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2546. return 0;
  2547. cpu_buffer = buffer->buffers[cpu];
  2548. ret = local_read(&cpu_buffer->commit_overrun);
  2549. return ret;
  2550. }
  2551. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  2552. /**
  2553. * ring_buffer_entries - get the number of entries in a buffer
  2554. * @buffer: The ring buffer
  2555. *
  2556. * Returns the total number of entries in the ring buffer
  2557. * (all CPU entries)
  2558. */
  2559. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  2560. {
  2561. struct ring_buffer_per_cpu *cpu_buffer;
  2562. unsigned long entries = 0;
  2563. int cpu;
  2564. /* if you care about this being correct, lock the buffer */
  2565. for_each_buffer_cpu(buffer, cpu) {
  2566. cpu_buffer = buffer->buffers[cpu];
  2567. entries += rb_num_of_entries(cpu_buffer);
  2568. }
  2569. return entries;
  2570. }
  2571. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  2572. /**
  2573. * ring_buffer_overruns - get the number of overruns in buffer
  2574. * @buffer: The ring buffer
  2575. *
  2576. * Returns the total number of overruns in the ring buffer
  2577. * (all CPU entries)
  2578. */
  2579. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  2580. {
  2581. struct ring_buffer_per_cpu *cpu_buffer;
  2582. unsigned long overruns = 0;
  2583. int cpu;
  2584. /* if you care about this being correct, lock the buffer */
  2585. for_each_buffer_cpu(buffer, cpu) {
  2586. cpu_buffer = buffer->buffers[cpu];
  2587. overruns += local_read(&cpu_buffer->overrun);
  2588. }
  2589. return overruns;
  2590. }
  2591. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  2592. static void rb_iter_reset(struct ring_buffer_iter *iter)
  2593. {
  2594. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2595. /* Iterator usage is expected to have record disabled */
  2596. if (list_empty(&cpu_buffer->reader_page->list)) {
  2597. iter->head_page = rb_set_head_page(cpu_buffer);
  2598. if (unlikely(!iter->head_page))
  2599. return;
  2600. iter->head = iter->head_page->read;
  2601. } else {
  2602. iter->head_page = cpu_buffer->reader_page;
  2603. iter->head = cpu_buffer->reader_page->read;
  2604. }
  2605. if (iter->head)
  2606. iter->read_stamp = cpu_buffer->read_stamp;
  2607. else
  2608. iter->read_stamp = iter->head_page->page->time_stamp;
  2609. iter->cache_reader_page = cpu_buffer->reader_page;
  2610. iter->cache_read = cpu_buffer->read;
  2611. }
  2612. /**
  2613. * ring_buffer_iter_reset - reset an iterator
  2614. * @iter: The iterator to reset
  2615. *
  2616. * Resets the iterator, so that it will start from the beginning
  2617. * again.
  2618. */
  2619. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  2620. {
  2621. struct ring_buffer_per_cpu *cpu_buffer;
  2622. unsigned long flags;
  2623. if (!iter)
  2624. return;
  2625. cpu_buffer = iter->cpu_buffer;
  2626. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2627. rb_iter_reset(iter);
  2628. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2629. }
  2630. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  2631. /**
  2632. * ring_buffer_iter_empty - check if an iterator has no more to read
  2633. * @iter: The iterator to check
  2634. */
  2635. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  2636. {
  2637. struct ring_buffer_per_cpu *cpu_buffer;
  2638. cpu_buffer = iter->cpu_buffer;
  2639. return iter->head_page == cpu_buffer->commit_page &&
  2640. iter->head == rb_commit_index(cpu_buffer);
  2641. }
  2642. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  2643. static void
  2644. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2645. struct ring_buffer_event *event)
  2646. {
  2647. u64 delta;
  2648. switch (event->type_len) {
  2649. case RINGBUF_TYPE_PADDING:
  2650. return;
  2651. case RINGBUF_TYPE_TIME_EXTEND:
  2652. delta = event->array[0];
  2653. delta <<= TS_SHIFT;
  2654. delta += event->time_delta;
  2655. cpu_buffer->read_stamp += delta;
  2656. return;
  2657. case RINGBUF_TYPE_TIME_STAMP:
  2658. /* FIXME: not implemented */
  2659. return;
  2660. case RINGBUF_TYPE_DATA:
  2661. cpu_buffer->read_stamp += event->time_delta;
  2662. return;
  2663. default:
  2664. BUG();
  2665. }
  2666. return;
  2667. }
  2668. static void
  2669. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  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. iter->read_stamp += delta;
  2681. return;
  2682. case RINGBUF_TYPE_TIME_STAMP:
  2683. /* FIXME: not implemented */
  2684. return;
  2685. case RINGBUF_TYPE_DATA:
  2686. iter->read_stamp += event->time_delta;
  2687. return;
  2688. default:
  2689. BUG();
  2690. }
  2691. return;
  2692. }
  2693. static struct buffer_page *
  2694. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  2695. {
  2696. struct buffer_page *reader = NULL;
  2697. unsigned long overwrite;
  2698. unsigned long flags;
  2699. int nr_loops = 0;
  2700. int ret;
  2701. local_irq_save(flags);
  2702. arch_spin_lock(&cpu_buffer->lock);
  2703. again:
  2704. /*
  2705. * This should normally only loop twice. But because the
  2706. * start of the reader inserts an empty page, it causes
  2707. * a case where we will loop three times. There should be no
  2708. * reason to loop four times (that I know of).
  2709. */
  2710. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  2711. reader = NULL;
  2712. goto out;
  2713. }
  2714. reader = cpu_buffer->reader_page;
  2715. /* If there's more to read, return this page */
  2716. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  2717. goto out;
  2718. /* Never should we have an index greater than the size */
  2719. if (RB_WARN_ON(cpu_buffer,
  2720. cpu_buffer->reader_page->read > rb_page_size(reader)))
  2721. goto out;
  2722. /* check if we caught up to the tail */
  2723. reader = NULL;
  2724. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  2725. goto out;
  2726. /*
  2727. * Reset the reader page to size zero.
  2728. */
  2729. local_set(&cpu_buffer->reader_page->write, 0);
  2730. local_set(&cpu_buffer->reader_page->entries, 0);
  2731. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2732. cpu_buffer->reader_page->real_end = 0;
  2733. spin:
  2734. /*
  2735. * Splice the empty reader page into the list around the head.
  2736. */
  2737. reader = rb_set_head_page(cpu_buffer);
  2738. cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
  2739. cpu_buffer->reader_page->list.prev = reader->list.prev;
  2740. /*
  2741. * cpu_buffer->pages just needs to point to the buffer, it
  2742. * has no specific buffer page to point to. Lets move it out
  2743. * of our way so we don't accidentally swap it.
  2744. */
  2745. cpu_buffer->pages = reader->list.prev;
  2746. /* The reader page will be pointing to the new head */
  2747. rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
  2748. /*
  2749. * We want to make sure we read the overruns after we set up our
  2750. * pointers to the next object. The writer side does a
  2751. * cmpxchg to cross pages which acts as the mb on the writer
  2752. * side. Note, the reader will constantly fail the swap
  2753. * while the writer is updating the pointers, so this
  2754. * guarantees that the overwrite recorded here is the one we
  2755. * want to compare with the last_overrun.
  2756. */
  2757. smp_mb();
  2758. overwrite = local_read(&(cpu_buffer->overrun));
  2759. /*
  2760. * Here's the tricky part.
  2761. *
  2762. * We need to move the pointer past the header page.
  2763. * But we can only do that if a writer is not currently
  2764. * moving it. The page before the header page has the
  2765. * flag bit '1' set if it is pointing to the page we want.
  2766. * but if the writer is in the process of moving it
  2767. * than it will be '2' or already moved '0'.
  2768. */
  2769. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  2770. /*
  2771. * If we did not convert it, then we must try again.
  2772. */
  2773. if (!ret)
  2774. goto spin;
  2775. /*
  2776. * Yeah! We succeeded in replacing the page.
  2777. *
  2778. * Now make the new head point back to the reader page.
  2779. */
  2780. rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
  2781. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  2782. /* Finally update the reader page to the new head */
  2783. cpu_buffer->reader_page = reader;
  2784. rb_reset_reader_page(cpu_buffer);
  2785. if (overwrite != cpu_buffer->last_overrun) {
  2786. cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
  2787. cpu_buffer->last_overrun = overwrite;
  2788. }
  2789. goto again;
  2790. out:
  2791. arch_spin_unlock(&cpu_buffer->lock);
  2792. local_irq_restore(flags);
  2793. return reader;
  2794. }
  2795. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  2796. {
  2797. struct ring_buffer_event *event;
  2798. struct buffer_page *reader;
  2799. unsigned length;
  2800. reader = rb_get_reader_page(cpu_buffer);
  2801. /* This function should not be called when buffer is empty */
  2802. if (RB_WARN_ON(cpu_buffer, !reader))
  2803. return;
  2804. event = rb_reader_event(cpu_buffer);
  2805. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  2806. cpu_buffer->read++;
  2807. rb_update_read_stamp(cpu_buffer, event);
  2808. length = rb_event_length(event);
  2809. cpu_buffer->reader_page->read += length;
  2810. }
  2811. static void rb_advance_iter(struct ring_buffer_iter *iter)
  2812. {
  2813. struct ring_buffer_per_cpu *cpu_buffer;
  2814. struct ring_buffer_event *event;
  2815. unsigned length;
  2816. cpu_buffer = iter->cpu_buffer;
  2817. /*
  2818. * Check if we are at the end of the buffer.
  2819. */
  2820. if (iter->head >= rb_page_size(iter->head_page)) {
  2821. /* discarded commits can make the page empty */
  2822. if (iter->head_page == cpu_buffer->commit_page)
  2823. return;
  2824. rb_inc_iter(iter);
  2825. return;
  2826. }
  2827. event = rb_iter_head_event(iter);
  2828. length = rb_event_length(event);
  2829. /*
  2830. * This should not be called to advance the header if we are
  2831. * at the tail of the buffer.
  2832. */
  2833. if (RB_WARN_ON(cpu_buffer,
  2834. (iter->head_page == cpu_buffer->commit_page) &&
  2835. (iter->head + length > rb_commit_index(cpu_buffer))))
  2836. return;
  2837. rb_update_iter_read_stamp(iter, event);
  2838. iter->head += length;
  2839. /* check for end of page padding */
  2840. if ((iter->head >= rb_page_size(iter->head_page)) &&
  2841. (iter->head_page != cpu_buffer->commit_page))
  2842. rb_advance_iter(iter);
  2843. }
  2844. static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
  2845. {
  2846. return cpu_buffer->lost_events;
  2847. }
  2848. static struct ring_buffer_event *
  2849. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
  2850. unsigned long *lost_events)
  2851. {
  2852. struct ring_buffer_event *event;
  2853. struct buffer_page *reader;
  2854. int nr_loops = 0;
  2855. again:
  2856. /*
  2857. * We repeat when a time extend is encountered.
  2858. * Since the time extend is always attached to a data event,
  2859. * we should never loop more than once.
  2860. * (We never hit the following condition more than twice).
  2861. */
  2862. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
  2863. return NULL;
  2864. reader = rb_get_reader_page(cpu_buffer);
  2865. if (!reader)
  2866. return NULL;
  2867. event = rb_reader_event(cpu_buffer);
  2868. switch (event->type_len) {
  2869. case RINGBUF_TYPE_PADDING:
  2870. if (rb_null_event(event))
  2871. RB_WARN_ON(cpu_buffer, 1);
  2872. /*
  2873. * Because the writer could be discarding every
  2874. * event it creates (which would probably be bad)
  2875. * if we were to go back to "again" then we may never
  2876. * catch up, and will trigger the warn on, or lock
  2877. * the box. Return the padding, and we will release
  2878. * the current locks, and try again.
  2879. */
  2880. return event;
  2881. case RINGBUF_TYPE_TIME_EXTEND:
  2882. /* Internal data, OK to advance */
  2883. rb_advance_reader(cpu_buffer);
  2884. goto again;
  2885. case RINGBUF_TYPE_TIME_STAMP:
  2886. /* FIXME: not implemented */
  2887. rb_advance_reader(cpu_buffer);
  2888. goto again;
  2889. case RINGBUF_TYPE_DATA:
  2890. if (ts) {
  2891. *ts = cpu_buffer->read_stamp + event->time_delta;
  2892. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  2893. cpu_buffer->cpu, ts);
  2894. }
  2895. if (lost_events)
  2896. *lost_events = rb_lost_events(cpu_buffer);
  2897. return event;
  2898. default:
  2899. BUG();
  2900. }
  2901. return NULL;
  2902. }
  2903. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  2904. static struct ring_buffer_event *
  2905. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2906. {
  2907. struct ring_buffer *buffer;
  2908. struct ring_buffer_per_cpu *cpu_buffer;
  2909. struct ring_buffer_event *event;
  2910. int nr_loops = 0;
  2911. cpu_buffer = iter->cpu_buffer;
  2912. buffer = cpu_buffer->buffer;
  2913. /*
  2914. * Check if someone performed a consuming read to
  2915. * the buffer. A consuming read invalidates the iterator
  2916. * and we need to reset the iterator in this case.
  2917. */
  2918. if (unlikely(iter->cache_read != cpu_buffer->read ||
  2919. iter->cache_reader_page != cpu_buffer->reader_page))
  2920. rb_iter_reset(iter);
  2921. again:
  2922. if (ring_buffer_iter_empty(iter))
  2923. return NULL;
  2924. /*
  2925. * We repeat when a time extend is encountered.
  2926. * Since the time extend is always attached to a data event,
  2927. * we should never loop more than once.
  2928. * (We never hit the following condition more than twice).
  2929. */
  2930. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
  2931. return NULL;
  2932. if (rb_per_cpu_empty(cpu_buffer))
  2933. return NULL;
  2934. if (iter->head >= local_read(&iter->head_page->page->commit)) {
  2935. rb_inc_iter(iter);
  2936. goto again;
  2937. }
  2938. event = rb_iter_head_event(iter);
  2939. switch (event->type_len) {
  2940. case RINGBUF_TYPE_PADDING:
  2941. if (rb_null_event(event)) {
  2942. rb_inc_iter(iter);
  2943. goto again;
  2944. }
  2945. rb_advance_iter(iter);
  2946. return event;
  2947. case RINGBUF_TYPE_TIME_EXTEND:
  2948. /* Internal data, OK to advance */
  2949. rb_advance_iter(iter);
  2950. goto again;
  2951. case RINGBUF_TYPE_TIME_STAMP:
  2952. /* FIXME: not implemented */
  2953. rb_advance_iter(iter);
  2954. goto again;
  2955. case RINGBUF_TYPE_DATA:
  2956. if (ts) {
  2957. *ts = iter->read_stamp + event->time_delta;
  2958. ring_buffer_normalize_time_stamp(buffer,
  2959. cpu_buffer->cpu, ts);
  2960. }
  2961. return event;
  2962. default:
  2963. BUG();
  2964. }
  2965. return NULL;
  2966. }
  2967. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2968. static inline int rb_ok_to_lock(void)
  2969. {
  2970. /*
  2971. * If an NMI die dumps out the content of the ring buffer
  2972. * do not grab locks. We also permanently disable the ring
  2973. * buffer too. A one time deal is all you get from reading
  2974. * the ring buffer from an NMI.
  2975. */
  2976. if (likely(!in_nmi()))
  2977. return 1;
  2978. tracing_off_permanent();
  2979. return 0;
  2980. }
  2981. /**
  2982. * ring_buffer_peek - peek at the next event to be read
  2983. * @buffer: The ring buffer to read
  2984. * @cpu: The cpu to peak at
  2985. * @ts: The timestamp counter of this event.
  2986. * @lost_events: a variable to store if events were lost (may be NULL)
  2987. *
  2988. * This will return the event that will be read next, but does
  2989. * not consume the data.
  2990. */
  2991. struct ring_buffer_event *
  2992. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
  2993. unsigned long *lost_events)
  2994. {
  2995. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2996. struct ring_buffer_event *event;
  2997. unsigned long flags;
  2998. int dolock;
  2999. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3000. return NULL;
  3001. dolock = rb_ok_to_lock();
  3002. again:
  3003. local_irq_save(flags);
  3004. if (dolock)
  3005. raw_spin_lock(&cpu_buffer->reader_lock);
  3006. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  3007. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  3008. rb_advance_reader(cpu_buffer);
  3009. if (dolock)
  3010. raw_spin_unlock(&cpu_buffer->reader_lock);
  3011. local_irq_restore(flags);
  3012. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  3013. goto again;
  3014. return event;
  3015. }
  3016. /**
  3017. * ring_buffer_iter_peek - peek at the next event to be read
  3018. * @iter: The ring buffer iterator
  3019. * @ts: The timestamp counter of this event.
  3020. *
  3021. * This will return the event that will be read next, but does
  3022. * not increment the iterator.
  3023. */
  3024. struct ring_buffer_event *
  3025. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  3026. {
  3027. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  3028. struct ring_buffer_event *event;
  3029. unsigned long flags;
  3030. again:
  3031. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3032. event = rb_iter_peek(iter, ts);
  3033. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3034. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  3035. goto again;
  3036. return event;
  3037. }
  3038. /**
  3039. * ring_buffer_consume - return an event and consume it
  3040. * @buffer: The ring buffer to get the next event from
  3041. * @cpu: the cpu to read the buffer from
  3042. * @ts: a variable to store the timestamp (may be NULL)
  3043. * @lost_events: a variable to store if events were lost (may be NULL)
  3044. *
  3045. * Returns the next event in the ring buffer, and that event is consumed.
  3046. * Meaning, that sequential reads will keep returning a different event,
  3047. * and eventually empty the ring buffer if the producer is slower.
  3048. */
  3049. struct ring_buffer_event *
  3050. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
  3051. unsigned long *lost_events)
  3052. {
  3053. struct ring_buffer_per_cpu *cpu_buffer;
  3054. struct ring_buffer_event *event = NULL;
  3055. unsigned long flags;
  3056. int dolock;
  3057. dolock = rb_ok_to_lock();
  3058. again:
  3059. /* might be called in atomic */
  3060. preempt_disable();
  3061. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3062. goto out;
  3063. cpu_buffer = buffer->buffers[cpu];
  3064. local_irq_save(flags);
  3065. if (dolock)
  3066. raw_spin_lock(&cpu_buffer->reader_lock);
  3067. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  3068. if (event) {
  3069. cpu_buffer->lost_events = 0;
  3070. rb_advance_reader(cpu_buffer);
  3071. }
  3072. if (dolock)
  3073. raw_spin_unlock(&cpu_buffer->reader_lock);
  3074. local_irq_restore(flags);
  3075. out:
  3076. preempt_enable();
  3077. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  3078. goto again;
  3079. return event;
  3080. }
  3081. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  3082. /**
  3083. * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
  3084. * @buffer: The ring buffer to read from
  3085. * @cpu: The cpu buffer to iterate over
  3086. *
  3087. * This performs the initial preparations necessary to iterate
  3088. * through the buffer. Memory is allocated, buffer recording
  3089. * is disabled, and the iterator pointer is returned to the caller.
  3090. *
  3091. * Disabling buffer recordng prevents the reading from being
  3092. * corrupted. This is not a consuming read, so a producer is not
  3093. * expected.
  3094. *
  3095. * After a sequence of ring_buffer_read_prepare calls, the user is
  3096. * expected to make at least one call to ring_buffer_prepare_sync.
  3097. * Afterwards, ring_buffer_read_start is invoked to get things going
  3098. * for real.
  3099. *
  3100. * This overall must be paired with ring_buffer_finish.
  3101. */
  3102. struct ring_buffer_iter *
  3103. ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
  3104. {
  3105. struct ring_buffer_per_cpu *cpu_buffer;
  3106. struct ring_buffer_iter *iter;
  3107. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3108. return NULL;
  3109. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  3110. if (!iter)
  3111. return NULL;
  3112. cpu_buffer = buffer->buffers[cpu];
  3113. iter->cpu_buffer = cpu_buffer;
  3114. atomic_inc(&buffer->resize_disabled);
  3115. atomic_inc(&cpu_buffer->record_disabled);
  3116. return iter;
  3117. }
  3118. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
  3119. /**
  3120. * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
  3121. *
  3122. * All previously invoked ring_buffer_read_prepare calls to prepare
  3123. * iterators will be synchronized. Afterwards, read_buffer_read_start
  3124. * calls on those iterators are allowed.
  3125. */
  3126. void
  3127. ring_buffer_read_prepare_sync(void)
  3128. {
  3129. synchronize_sched();
  3130. }
  3131. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
  3132. /**
  3133. * ring_buffer_read_start - start a non consuming read of the buffer
  3134. * @iter: The iterator returned by ring_buffer_read_prepare
  3135. *
  3136. * This finalizes the startup of an iteration through the buffer.
  3137. * The iterator comes from a call to ring_buffer_read_prepare and
  3138. * an intervening ring_buffer_read_prepare_sync must have been
  3139. * performed.
  3140. *
  3141. * Must be paired with ring_buffer_finish.
  3142. */
  3143. void
  3144. ring_buffer_read_start(struct ring_buffer_iter *iter)
  3145. {
  3146. struct ring_buffer_per_cpu *cpu_buffer;
  3147. unsigned long flags;
  3148. if (!iter)
  3149. return;
  3150. cpu_buffer = iter->cpu_buffer;
  3151. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3152. arch_spin_lock(&cpu_buffer->lock);
  3153. rb_iter_reset(iter);
  3154. arch_spin_unlock(&cpu_buffer->lock);
  3155. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3156. }
  3157. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  3158. /**
  3159. * ring_buffer_finish - finish reading the iterator of the buffer
  3160. * @iter: The iterator retrieved by ring_buffer_start
  3161. *
  3162. * This re-enables the recording to the buffer, and frees the
  3163. * iterator.
  3164. */
  3165. void
  3166. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  3167. {
  3168. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  3169. atomic_dec(&cpu_buffer->record_disabled);
  3170. atomic_dec(&cpu_buffer->buffer->resize_disabled);
  3171. kfree(iter);
  3172. }
  3173. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  3174. /**
  3175. * ring_buffer_read - read the next item in the ring buffer by the iterator
  3176. * @iter: The ring buffer iterator
  3177. * @ts: The time stamp of the event read.
  3178. *
  3179. * This reads the next event in the ring buffer and increments the iterator.
  3180. */
  3181. struct ring_buffer_event *
  3182. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  3183. {
  3184. struct ring_buffer_event *event;
  3185. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  3186. unsigned long flags;
  3187. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3188. again:
  3189. event = rb_iter_peek(iter, ts);
  3190. if (!event)
  3191. goto out;
  3192. if (event->type_len == RINGBUF_TYPE_PADDING)
  3193. goto again;
  3194. rb_advance_iter(iter);
  3195. out:
  3196. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3197. return event;
  3198. }
  3199. EXPORT_SYMBOL_GPL(ring_buffer_read);
  3200. /**
  3201. * ring_buffer_size - return the size of the ring buffer (in bytes)
  3202. * @buffer: The ring buffer.
  3203. */
  3204. unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
  3205. {
  3206. /*
  3207. * Earlier, this method returned
  3208. * BUF_PAGE_SIZE * buffer->nr_pages
  3209. * Since the nr_pages field is now removed, we have converted this to
  3210. * return the per cpu buffer value.
  3211. */
  3212. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3213. return 0;
  3214. return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
  3215. }
  3216. EXPORT_SYMBOL_GPL(ring_buffer_size);
  3217. static void
  3218. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  3219. {
  3220. rb_head_page_deactivate(cpu_buffer);
  3221. cpu_buffer->head_page
  3222. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  3223. local_set(&cpu_buffer->head_page->write, 0);
  3224. local_set(&cpu_buffer->head_page->entries, 0);
  3225. local_set(&cpu_buffer->head_page->page->commit, 0);
  3226. cpu_buffer->head_page->read = 0;
  3227. cpu_buffer->tail_page = cpu_buffer->head_page;
  3228. cpu_buffer->commit_page = cpu_buffer->head_page;
  3229. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  3230. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  3231. local_set(&cpu_buffer->reader_page->write, 0);
  3232. local_set(&cpu_buffer->reader_page->entries, 0);
  3233. local_set(&cpu_buffer->reader_page->page->commit, 0);
  3234. cpu_buffer->reader_page->read = 0;
  3235. local_set(&cpu_buffer->commit_overrun, 0);
  3236. local_set(&cpu_buffer->entries_bytes, 0);
  3237. local_set(&cpu_buffer->overrun, 0);
  3238. local_set(&cpu_buffer->entries, 0);
  3239. local_set(&cpu_buffer->committing, 0);
  3240. local_set(&cpu_buffer->commits, 0);
  3241. cpu_buffer->read = 0;
  3242. cpu_buffer->read_bytes = 0;
  3243. cpu_buffer->write_stamp = 0;
  3244. cpu_buffer->read_stamp = 0;
  3245. cpu_buffer->lost_events = 0;
  3246. cpu_buffer->last_overrun = 0;
  3247. rb_head_page_activate(cpu_buffer);
  3248. }
  3249. /**
  3250. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  3251. * @buffer: The ring buffer to reset a per cpu buffer of
  3252. * @cpu: The CPU buffer to be reset
  3253. */
  3254. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  3255. {
  3256. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  3257. unsigned long flags;
  3258. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3259. return;
  3260. atomic_inc(&buffer->resize_disabled);
  3261. atomic_inc(&cpu_buffer->record_disabled);
  3262. /* Make sure all commits have finished */
  3263. synchronize_sched();
  3264. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3265. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  3266. goto out;
  3267. arch_spin_lock(&cpu_buffer->lock);
  3268. rb_reset_cpu(cpu_buffer);
  3269. arch_spin_unlock(&cpu_buffer->lock);
  3270. out:
  3271. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3272. atomic_dec(&cpu_buffer->record_disabled);
  3273. atomic_dec(&buffer->resize_disabled);
  3274. }
  3275. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  3276. /**
  3277. * ring_buffer_reset - reset a ring buffer
  3278. * @buffer: The ring buffer to reset all cpu buffers
  3279. */
  3280. void ring_buffer_reset(struct ring_buffer *buffer)
  3281. {
  3282. int cpu;
  3283. for_each_buffer_cpu(buffer, cpu)
  3284. ring_buffer_reset_cpu(buffer, cpu);
  3285. }
  3286. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  3287. /**
  3288. * rind_buffer_empty - is the ring buffer empty?
  3289. * @buffer: The ring buffer to test
  3290. */
  3291. int ring_buffer_empty(struct ring_buffer *buffer)
  3292. {
  3293. struct ring_buffer_per_cpu *cpu_buffer;
  3294. unsigned long flags;
  3295. int dolock;
  3296. int cpu;
  3297. int ret;
  3298. dolock = rb_ok_to_lock();
  3299. /* yes this is racy, but if you don't like the race, lock the buffer */
  3300. for_each_buffer_cpu(buffer, cpu) {
  3301. cpu_buffer = buffer->buffers[cpu];
  3302. local_irq_save(flags);
  3303. if (dolock)
  3304. raw_spin_lock(&cpu_buffer->reader_lock);
  3305. ret = rb_per_cpu_empty(cpu_buffer);
  3306. if (dolock)
  3307. raw_spin_unlock(&cpu_buffer->reader_lock);
  3308. local_irq_restore(flags);
  3309. if (!ret)
  3310. return 0;
  3311. }
  3312. return 1;
  3313. }
  3314. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  3315. /**
  3316. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  3317. * @buffer: The ring buffer
  3318. * @cpu: The CPU buffer to test
  3319. */
  3320. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  3321. {
  3322. struct ring_buffer_per_cpu *cpu_buffer;
  3323. unsigned long flags;
  3324. int dolock;
  3325. int ret;
  3326. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3327. return 1;
  3328. dolock = rb_ok_to_lock();
  3329. cpu_buffer = buffer->buffers[cpu];
  3330. local_irq_save(flags);
  3331. if (dolock)
  3332. raw_spin_lock(&cpu_buffer->reader_lock);
  3333. ret = rb_per_cpu_empty(cpu_buffer);
  3334. if (dolock)
  3335. raw_spin_unlock(&cpu_buffer->reader_lock);
  3336. local_irq_restore(flags);
  3337. return ret;
  3338. }
  3339. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  3340. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3341. /**
  3342. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  3343. * @buffer_a: One buffer to swap with
  3344. * @buffer_b: The other buffer to swap with
  3345. *
  3346. * This function is useful for tracers that want to take a "snapshot"
  3347. * of a CPU buffer and has another back up buffer lying around.
  3348. * it is expected that the tracer handles the cpu buffer not being
  3349. * used at the moment.
  3350. */
  3351. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  3352. struct ring_buffer *buffer_b, int cpu)
  3353. {
  3354. struct ring_buffer_per_cpu *cpu_buffer_a;
  3355. struct ring_buffer_per_cpu *cpu_buffer_b;
  3356. int ret = -EINVAL;
  3357. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  3358. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  3359. goto out;
  3360. cpu_buffer_a = buffer_a->buffers[cpu];
  3361. cpu_buffer_b = buffer_b->buffers[cpu];
  3362. /* At least make sure the two buffers are somewhat the same */
  3363. if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
  3364. goto out;
  3365. ret = -EAGAIN;
  3366. if (ring_buffer_flags != RB_BUFFERS_ON)
  3367. goto out;
  3368. if (atomic_read(&buffer_a->record_disabled))
  3369. goto out;
  3370. if (atomic_read(&buffer_b->record_disabled))
  3371. goto out;
  3372. if (atomic_read(&cpu_buffer_a->record_disabled))
  3373. goto out;
  3374. if (atomic_read(&cpu_buffer_b->record_disabled))
  3375. goto out;
  3376. /*
  3377. * We can't do a synchronize_sched here because this
  3378. * function can be called in atomic context.
  3379. * Normally this will be called from the same CPU as cpu.
  3380. * If not it's up to the caller to protect this.
  3381. */
  3382. atomic_inc(&cpu_buffer_a->record_disabled);
  3383. atomic_inc(&cpu_buffer_b->record_disabled);
  3384. ret = -EBUSY;
  3385. if (local_read(&cpu_buffer_a->committing))
  3386. goto out_dec;
  3387. if (local_read(&cpu_buffer_b->committing))
  3388. goto out_dec;
  3389. buffer_a->buffers[cpu] = cpu_buffer_b;
  3390. buffer_b->buffers[cpu] = cpu_buffer_a;
  3391. cpu_buffer_b->buffer = buffer_a;
  3392. cpu_buffer_a->buffer = buffer_b;
  3393. ret = 0;
  3394. out_dec:
  3395. atomic_dec(&cpu_buffer_a->record_disabled);
  3396. atomic_dec(&cpu_buffer_b->record_disabled);
  3397. out:
  3398. return ret;
  3399. }
  3400. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  3401. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  3402. /**
  3403. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  3404. * @buffer: the buffer to allocate for.
  3405. *
  3406. * This function is used in conjunction with ring_buffer_read_page.
  3407. * When reading a full page from the ring buffer, these functions
  3408. * can be used to speed up the process. The calling function should
  3409. * allocate a few pages first with this function. Then when it
  3410. * needs to get pages from the ring buffer, it passes the result
  3411. * of this function into ring_buffer_read_page, which will swap
  3412. * the page that was allocated, with the read page of the buffer.
  3413. *
  3414. * Returns:
  3415. * The page allocated, or NULL on error.
  3416. */
  3417. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
  3418. {
  3419. struct buffer_data_page *bpage;
  3420. struct page *page;
  3421. page = alloc_pages_node(cpu_to_node(cpu),
  3422. GFP_KERNEL | __GFP_NORETRY, 0);
  3423. if (!page)
  3424. return NULL;
  3425. bpage = page_address(page);
  3426. rb_init_page(bpage);
  3427. return bpage;
  3428. }
  3429. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  3430. /**
  3431. * ring_buffer_free_read_page - free an allocated read page
  3432. * @buffer: the buffer the page was allocate for
  3433. * @data: the page to free
  3434. *
  3435. * Free a page allocated from ring_buffer_alloc_read_page.
  3436. */
  3437. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  3438. {
  3439. free_page((unsigned long)data);
  3440. }
  3441. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  3442. /**
  3443. * ring_buffer_read_page - extract a page from the ring buffer
  3444. * @buffer: buffer to extract from
  3445. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  3446. * @len: amount to extract
  3447. * @cpu: the cpu of the buffer to extract
  3448. * @full: should the extraction only happen when the page is full.
  3449. *
  3450. * This function will pull out a page from the ring buffer and consume it.
  3451. * @data_page must be the address of the variable that was returned
  3452. * from ring_buffer_alloc_read_page. This is because the page might be used
  3453. * to swap with a page in the ring buffer.
  3454. *
  3455. * for example:
  3456. * rpage = ring_buffer_alloc_read_page(buffer);
  3457. * if (!rpage)
  3458. * return error;
  3459. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  3460. * if (ret >= 0)
  3461. * process_page(rpage, ret);
  3462. *
  3463. * When @full is set, the function will not return true unless
  3464. * the writer is off the reader page.
  3465. *
  3466. * Note: it is up to the calling functions to handle sleeps and wakeups.
  3467. * The ring buffer can be used anywhere in the kernel and can not
  3468. * blindly call wake_up. The layer that uses the ring buffer must be
  3469. * responsible for that.
  3470. *
  3471. * Returns:
  3472. * >=0 if data has been transferred, returns the offset of consumed data.
  3473. * <0 if no data has been transferred.
  3474. */
  3475. int ring_buffer_read_page(struct ring_buffer *buffer,
  3476. void **data_page, size_t len, int cpu, int full)
  3477. {
  3478. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  3479. struct ring_buffer_event *event;
  3480. struct buffer_data_page *bpage;
  3481. struct buffer_page *reader;
  3482. unsigned long missed_events;
  3483. unsigned long flags;
  3484. unsigned int commit;
  3485. unsigned int read;
  3486. u64 save_timestamp;
  3487. int ret = -1;
  3488. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3489. goto out;
  3490. /*
  3491. * If len is not big enough to hold the page header, then
  3492. * we can not copy anything.
  3493. */
  3494. if (len <= BUF_PAGE_HDR_SIZE)
  3495. goto out;
  3496. len -= BUF_PAGE_HDR_SIZE;
  3497. if (!data_page)
  3498. goto out;
  3499. bpage = *data_page;
  3500. if (!bpage)
  3501. goto out;
  3502. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3503. reader = rb_get_reader_page(cpu_buffer);
  3504. if (!reader)
  3505. goto out_unlock;
  3506. event = rb_reader_event(cpu_buffer);
  3507. read = reader->read;
  3508. commit = rb_page_commit(reader);
  3509. /* Check if any events were dropped */
  3510. missed_events = cpu_buffer->lost_events;
  3511. /*
  3512. * If this page has been partially read or
  3513. * if len is not big enough to read the rest of the page or
  3514. * a writer is still on the page, then
  3515. * we must copy the data from the page to the buffer.
  3516. * Otherwise, we can simply swap the page with the one passed in.
  3517. */
  3518. if (read || (len < (commit - read)) ||
  3519. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  3520. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  3521. unsigned int rpos = read;
  3522. unsigned int pos = 0;
  3523. unsigned int size;
  3524. if (full)
  3525. goto out_unlock;
  3526. if (len > (commit - read))
  3527. len = (commit - read);
  3528. /* Always keep the time extend and data together */
  3529. size = rb_event_ts_length(event);
  3530. if (len < size)
  3531. goto out_unlock;
  3532. /* save the current timestamp, since the user will need it */
  3533. save_timestamp = cpu_buffer->read_stamp;
  3534. /* Need to copy one event at a time */
  3535. do {
  3536. /* We need the size of one event, because
  3537. * rb_advance_reader only advances by one event,
  3538. * whereas rb_event_ts_length may include the size of
  3539. * one or two events.
  3540. * We have already ensured there's enough space if this
  3541. * is a time extend. */
  3542. size = rb_event_length(event);
  3543. memcpy(bpage->data + pos, rpage->data + rpos, size);
  3544. len -= size;
  3545. rb_advance_reader(cpu_buffer);
  3546. rpos = reader->read;
  3547. pos += size;
  3548. if (rpos >= commit)
  3549. break;
  3550. event = rb_reader_event(cpu_buffer);
  3551. /* Always keep the time extend and data together */
  3552. size = rb_event_ts_length(event);
  3553. } while (len >= size);
  3554. /* update bpage */
  3555. local_set(&bpage->commit, pos);
  3556. bpage->time_stamp = save_timestamp;
  3557. /* we copied everything to the beginning */
  3558. read = 0;
  3559. } else {
  3560. /* update the entry counter */
  3561. cpu_buffer->read += rb_page_entries(reader);
  3562. cpu_buffer->read_bytes += BUF_PAGE_SIZE;
  3563. /* swap the pages */
  3564. rb_init_page(bpage);
  3565. bpage = reader->page;
  3566. reader->page = *data_page;
  3567. local_set(&reader->write, 0);
  3568. local_set(&reader->entries, 0);
  3569. reader->read = 0;
  3570. *data_page = bpage;
  3571. /*
  3572. * Use the real_end for the data size,
  3573. * This gives us a chance to store the lost events
  3574. * on the page.
  3575. */
  3576. if (reader->real_end)
  3577. local_set(&bpage->commit, reader->real_end);
  3578. }
  3579. ret = read;
  3580. cpu_buffer->lost_events = 0;
  3581. commit = local_read(&bpage->commit);
  3582. /*
  3583. * Set a flag in the commit field if we lost events
  3584. */
  3585. if (missed_events) {
  3586. /* If there is room at the end of the page to save the
  3587. * missed events, then record it there.
  3588. */
  3589. if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
  3590. memcpy(&bpage->data[commit], &missed_events,
  3591. sizeof(missed_events));
  3592. local_add(RB_MISSED_STORED, &bpage->commit);
  3593. commit += sizeof(missed_events);
  3594. }
  3595. local_add(RB_MISSED_EVENTS, &bpage->commit);
  3596. }
  3597. /*
  3598. * This page may be off to user land. Zero it out here.
  3599. */
  3600. if (commit < BUF_PAGE_SIZE)
  3601. memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
  3602. out_unlock:
  3603. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3604. out:
  3605. return ret;
  3606. }
  3607. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  3608. #ifdef CONFIG_HOTPLUG_CPU
  3609. static int rb_cpu_notify(struct notifier_block *self,
  3610. unsigned long action, void *hcpu)
  3611. {
  3612. struct ring_buffer *buffer =
  3613. container_of(self, struct ring_buffer, cpu_notify);
  3614. long cpu = (long)hcpu;
  3615. int cpu_i, nr_pages_same;
  3616. unsigned int nr_pages;
  3617. switch (action) {
  3618. case CPU_UP_PREPARE:
  3619. case CPU_UP_PREPARE_FROZEN:
  3620. if (cpumask_test_cpu(cpu, buffer->cpumask))
  3621. return NOTIFY_OK;
  3622. nr_pages = 0;
  3623. nr_pages_same = 1;
  3624. /* check if all cpu sizes are same */
  3625. for_each_buffer_cpu(buffer, cpu_i) {
  3626. /* fill in the size from first enabled cpu */
  3627. if (nr_pages == 0)
  3628. nr_pages = buffer->buffers[cpu_i]->nr_pages;
  3629. if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
  3630. nr_pages_same = 0;
  3631. break;
  3632. }
  3633. }
  3634. /* allocate minimum pages, user can later expand it */
  3635. if (!nr_pages_same)
  3636. nr_pages = 2;
  3637. buffer->buffers[cpu] =
  3638. rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  3639. if (!buffer->buffers[cpu]) {
  3640. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  3641. cpu);
  3642. return NOTIFY_OK;
  3643. }
  3644. smp_wmb();
  3645. cpumask_set_cpu(cpu, buffer->cpumask);
  3646. break;
  3647. case CPU_DOWN_PREPARE:
  3648. case CPU_DOWN_PREPARE_FROZEN:
  3649. /*
  3650. * Do nothing.
  3651. * If we were to free the buffer, then the user would
  3652. * lose any trace that was in the buffer.
  3653. */
  3654. break;
  3655. default:
  3656. break;
  3657. }
  3658. return NOTIFY_OK;
  3659. }
  3660. #endif