ring_buffer.c 130 KB

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