ring_buffer.c 135 KB

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