ring_buffer.c 95 KB

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