ring_buffer.c 102 KB

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