ring_buffer.c 96 KB

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