ring_buffer.c 134 KB

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