ring_buffer.c 136 KB

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