ring_buffer.c 134 KB

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