ring_buffer.c 95 KB

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