ring_buffer.c 119 KB

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