ring_buffer.c 131 KB

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