ring_buffer.c 112 KB

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