ring_buffer.c 131 KB

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