ring_buffer.c 113 KB

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