ring_buffer.c 131 KB

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