ring_buffer.c 100 KB

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