ring_buffer.c 105 KB

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