ring_buffer.c 129 KB

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