ring_buffer.c 136 KB

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