ring_buffer.c 128 KB

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