ring_buffer.c 129 KB

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