ring_buffer.c 101 KB

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