ring_buffer.c 115 KB

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