ring_buffer.c 103 KB

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