ring_buffer.c 132 KB

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