ring_buffer.c 129 KB

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