ring_buffer.c 130 KB

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