ring_buffer.c 96 KB

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