ring_buffer.c 115 KB

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