ring_buffer.c 130 KB

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