ring_buffer.c 131 KB

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