ring_buffer.c 209 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831
  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/sched/isolation.h>
  8. #include <linux/trace_recursion.h>
  9. #include <linux/trace_events.h>
  10. #include <linux/ring_buffer.h>
  11. #include <linux/trace_clock.h>
  12. #include <linux/sched/clock.h>
  13. #include <linux/cacheflush.h>
  14. #include <linux/trace_seq.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/irq_work.h>
  17. #include <linux/security.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/kthread.h> /* for self test */
  21. #include <linux/module.h>
  22. #include <linux/percpu.h>
  23. #include <linux/mutex.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/hash.h>
  28. #include <linux/list.h>
  29. #include <linux/cpu.h>
  30. #include <linux/oom.h>
  31. #include <linux/mm.h>
  32. #include <asm/local64.h>
  33. #include <asm/local.h>
  34. #include <asm/setup.h>
  35. #include "trace.h"
  36. /*
  37. * The "absolute" timestamp in the buffer is only 59 bits.
  38. * If a clock has the 5 MSBs set, it needs to be saved and
  39. * reinserted.
  40. */
  41. #define TS_MSB (0xf8ULL << 56)
  42. #define ABS_TS_MASK (~TS_MSB)
  43. static void update_pages_handler(struct work_struct *work);
  44. #define RING_BUFFER_META_MAGIC 0xBADFEED
  45. struct ring_buffer_meta {
  46. int magic;
  47. int struct_sizes;
  48. unsigned long total_size;
  49. unsigned long buffers_offset;
  50. };
  51. struct ring_buffer_cpu_meta {
  52. unsigned long first_buffer;
  53. unsigned long head_buffer;
  54. unsigned long commit_buffer;
  55. __u32 subbuf_size;
  56. __u32 nr_subbufs;
  57. int buffers[];
  58. };
  59. /*
  60. * The ring buffer header is special. We must manually up keep it.
  61. */
  62. int ring_buffer_print_entry_header(struct trace_seq *s)
  63. {
  64. trace_seq_puts(s, "# compressed entry header\n");
  65. trace_seq_puts(s, "\ttype_len : 5 bits\n");
  66. trace_seq_puts(s, "\ttime_delta : 27 bits\n");
  67. trace_seq_puts(s, "\tarray : 32 bits\n");
  68. trace_seq_putc(s, '\n');
  69. trace_seq_printf(s, "\tpadding : type == %d\n",
  70. RINGBUF_TYPE_PADDING);
  71. trace_seq_printf(s, "\ttime_extend : type == %d\n",
  72. RINGBUF_TYPE_TIME_EXTEND);
  73. trace_seq_printf(s, "\ttime_stamp : type == %d\n",
  74. RINGBUF_TYPE_TIME_STAMP);
  75. trace_seq_printf(s, "\tdata max type_len == %d\n",
  76. RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  77. return !trace_seq_has_overflowed(s);
  78. }
  79. /*
  80. * The ring buffer is made up of a list of pages. A separate list of pages is
  81. * allocated for each CPU. A writer may only write to a buffer that is
  82. * associated with the CPU it is currently executing on. A reader may read
  83. * from any per cpu buffer.
  84. *
  85. * The reader is special. For each per cpu buffer, the reader has its own
  86. * reader page. When a reader has read the entire reader page, this reader
  87. * page is swapped with another page in the ring buffer.
  88. *
  89. * Now, as long as the writer is off the reader page, the reader can do what
  90. * ever it wants with that page. The writer will never write to that page
  91. * again (as long as it is out of the ring buffer).
  92. *
  93. * Here's some silly ASCII art.
  94. *
  95. * +------+
  96. * |reader| RING BUFFER
  97. * |page |
  98. * +------+ +---+ +---+ +---+
  99. * | |-->| |-->| |
  100. * +---+ +---+ +---+
  101. * ^ |
  102. * | |
  103. * +---------------+
  104. *
  105. *
  106. * +------+
  107. * |reader| RING BUFFER
  108. * |page |------------------v
  109. * +------+ +---+ +---+ +---+
  110. * | |-->| |-->| |
  111. * +---+ +---+ +---+
  112. * ^ |
  113. * | |
  114. * +---------------+
  115. *
  116. *
  117. * +------+
  118. * |reader| RING BUFFER
  119. * |page |------------------v
  120. * +------+ +---+ +---+ +---+
  121. * ^ | |-->| |-->| |
  122. * | +---+ +---+ +---+
  123. * | |
  124. * | |
  125. * +------------------------------+
  126. *
  127. *
  128. * +------+
  129. * |buffer| RING BUFFER
  130. * |page |------------------v
  131. * +------+ +---+ +---+ +---+
  132. * ^ | | | |-->| |
  133. * | New +---+ +---+ +---+
  134. * | Reader------^ |
  135. * | page |
  136. * +------------------------------+
  137. *
  138. *
  139. * After we make this swap, the reader can hand this page off to the splice
  140. * code and be done with it. It can even allocate a new page if it needs to
  141. * and swap that into the ring buffer.
  142. *
  143. * We will be using cmpxchg soon to make all this lockless.
  144. *
  145. */
  146. /* Used for individual buffers (after the counter) */
  147. #define RB_BUFFER_OFF (1 << 20)
  148. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  149. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  150. #define RB_ALIGNMENT 4U
  151. #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  152. #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
  153. #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
  154. # define RB_FORCE_8BYTE_ALIGNMENT 0
  155. # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
  156. #else
  157. # define RB_FORCE_8BYTE_ALIGNMENT 1
  158. # define RB_ARCH_ALIGNMENT 8U
  159. #endif
  160. #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
  161. /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
  162. #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
  163. enum {
  164. RB_LEN_TIME_EXTEND = 8,
  165. RB_LEN_TIME_STAMP = 8,
  166. };
  167. #define skip_time_extend(event) \
  168. ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
  169. #define extended_time(event) \
  170. (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
  171. static inline bool rb_null_event(struct ring_buffer_event *event)
  172. {
  173. return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
  174. }
  175. static void rb_event_set_padding(struct ring_buffer_event *event)
  176. {
  177. /* padding has a NULL time_delta */
  178. event->type_len = RINGBUF_TYPE_PADDING;
  179. event->time_delta = 0;
  180. }
  181. static unsigned
  182. rb_event_data_length(struct ring_buffer_event *event)
  183. {
  184. unsigned length;
  185. if (event->type_len)
  186. length = event->type_len * RB_ALIGNMENT;
  187. else
  188. length = event->array[0];
  189. return length + RB_EVNT_HDR_SIZE;
  190. }
  191. /*
  192. * Return the length of the given event. Will return
  193. * the length of the time extend if the event is a
  194. * time extend.
  195. */
  196. static inline unsigned
  197. rb_event_length(struct ring_buffer_event *event)
  198. {
  199. switch (event->type_len) {
  200. case RINGBUF_TYPE_PADDING:
  201. if (rb_null_event(event))
  202. /* undefined */
  203. return -1;
  204. return event->array[0] + RB_EVNT_HDR_SIZE;
  205. case RINGBUF_TYPE_TIME_EXTEND:
  206. return RB_LEN_TIME_EXTEND;
  207. case RINGBUF_TYPE_TIME_STAMP:
  208. return RB_LEN_TIME_STAMP;
  209. case RINGBUF_TYPE_DATA:
  210. return rb_event_data_length(event);
  211. default:
  212. WARN_ON_ONCE(1);
  213. }
  214. /* not hit */
  215. return 0;
  216. }
  217. /*
  218. * Return total length of time extend and data,
  219. * or just the event length for all other events.
  220. */
  221. static inline unsigned
  222. rb_event_ts_length(struct ring_buffer_event *event)
  223. {
  224. unsigned len = 0;
  225. if (extended_time(event)) {
  226. /* time extends include the data event after it */
  227. len = RB_LEN_TIME_EXTEND;
  228. event = skip_time_extend(event);
  229. }
  230. return len + rb_event_length(event);
  231. }
  232. /**
  233. * ring_buffer_event_length - return the length of the event
  234. * @event: the event to get the length of
  235. *
  236. * Returns the size of the data load of a data event.
  237. * If the event is something other than a data event, it
  238. * returns the size of the event itself. With the exception
  239. * of a TIME EXTEND, where it still returns the size of the
  240. * data load of the data event after it.
  241. */
  242. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  243. {
  244. unsigned length;
  245. if (extended_time(event))
  246. event = skip_time_extend(event);
  247. length = rb_event_length(event);
  248. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  249. return length;
  250. length -= RB_EVNT_HDR_SIZE;
  251. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  252. length -= sizeof(event->array[0]);
  253. return length;
  254. }
  255. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  256. /* inline for ring buffer fast paths */
  257. static __always_inline void *
  258. rb_event_data(struct ring_buffer_event *event)
  259. {
  260. if (extended_time(event))
  261. event = skip_time_extend(event);
  262. WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  263. /* If length is in len field, then array[0] has the data */
  264. if (event->type_len)
  265. return (void *)&event->array[0];
  266. /* Otherwise length is in array[0] and array[1] has the data */
  267. return (void *)&event->array[1];
  268. }
  269. /**
  270. * ring_buffer_event_data - return the data of the event
  271. * @event: the event to get the data from
  272. */
  273. void *ring_buffer_event_data(struct ring_buffer_event *event)
  274. {
  275. return rb_event_data(event);
  276. }
  277. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  278. #define for_each_buffer_cpu(buffer, cpu) \
  279. for_each_cpu(cpu, buffer->cpumask)
  280. #define for_each_online_buffer_cpu(buffer, cpu) \
  281. for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
  282. #define TS_SHIFT 27
  283. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  284. #define TS_DELTA_TEST (~TS_MASK)
  285. static u64 rb_event_time_stamp(struct ring_buffer_event *event)
  286. {
  287. u64 ts;
  288. ts = event->array[0];
  289. ts <<= TS_SHIFT;
  290. ts += event->time_delta;
  291. return ts;
  292. }
  293. /* Flag when events were overwritten */
  294. #define RB_MISSED_EVENTS (1 << 31)
  295. /* Missed count stored at end */
  296. #define RB_MISSED_STORED (1 << 30)
  297. #define RB_MISSED_MASK (3 << 30)
  298. struct buffer_data_page {
  299. u64 time_stamp; /* page time stamp */
  300. local_t commit; /* write committed index */
  301. unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
  302. };
  303. struct buffer_data_read_page {
  304. unsigned order; /* order of the page */
  305. struct buffer_data_page *data; /* actual data, stored in this page */
  306. };
  307. /*
  308. * Note, the buffer_page list must be first. The buffer pages
  309. * are allocated in cache lines, which means that each buffer
  310. * page will be at the beginning of a cache line, and thus
  311. * the least significant bits will be zero. We use this to
  312. * add flags in the list struct pointers, to make the ring buffer
  313. * lockless.
  314. */
  315. struct buffer_page {
  316. struct list_head list; /* list of buffer pages */
  317. local_t write; /* index for next write */
  318. unsigned read; /* index for next read */
  319. local_t entries; /* entries on this page */
  320. unsigned long real_end; /* real end of data */
  321. unsigned order; /* order of the page */
  322. u32 id:30; /* ID for external mapping */
  323. u32 range:1; /* Mapped via a range */
  324. struct buffer_data_page *page; /* Actual data page */
  325. };
  326. /*
  327. * The buffer page counters, write and entries, must be reset
  328. * atomically when crossing page boundaries. To synchronize this
  329. * update, two counters are inserted into the number. One is
  330. * the actual counter for the write position or count on the page.
  331. *
  332. * The other is a counter of updaters. Before an update happens
  333. * the update partition of the counter is incremented. This will
  334. * allow the updater to update the counter atomically.
  335. *
  336. * The counter is 20 bits, and the state data is 12.
  337. */
  338. #define RB_WRITE_MASK 0xfffff
  339. #define RB_WRITE_INTCNT (1 << 20)
  340. static void rb_init_page(struct buffer_data_page *bpage)
  341. {
  342. local_set(&bpage->commit, 0);
  343. }
  344. static __always_inline unsigned int rb_page_commit(struct buffer_page *bpage)
  345. {
  346. return local_read(&bpage->page->commit);
  347. }
  348. static void free_buffer_page(struct buffer_page *bpage)
  349. {
  350. /* Range pages are not to be freed */
  351. if (!bpage->range)
  352. free_pages((unsigned long)bpage->page, bpage->order);
  353. kfree(bpage);
  354. }
  355. /*
  356. * For best performance, allocate cpu buffer data cache line sized
  357. * and per CPU.
  358. */
  359. #define alloc_cpu_buffer(cpu) (struct ring_buffer_per_cpu *) \
  360. kzalloc_node(ALIGN(sizeof(struct ring_buffer_per_cpu), \
  361. cache_line_size()), GFP_KERNEL, cpu_to_node(cpu));
  362. #define alloc_cpu_page(cpu) (struct buffer_page *) \
  363. kzalloc_node(ALIGN(sizeof(struct buffer_page), \
  364. cache_line_size()), GFP_KERNEL, cpu_to_node(cpu));
  365. static struct buffer_data_page *alloc_cpu_data(int cpu, int order)
  366. {
  367. struct buffer_data_page *dpage;
  368. struct page *page;
  369. gfp_t mflags;
  370. /*
  371. * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
  372. * gracefully without invoking oom-killer and the system is not
  373. * destabilized.
  374. */
  375. mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;
  376. page = alloc_pages_node(cpu_to_node(cpu), mflags, order);
  377. if (!page)
  378. return NULL;
  379. dpage = page_address(page);
  380. rb_init_page(dpage);
  381. return dpage;
  382. }
  383. /*
  384. * We need to fit the time_stamp delta into 27 bits.
  385. */
  386. static inline bool test_time_stamp(u64 delta)
  387. {
  388. return !!(delta & TS_DELTA_TEST);
  389. }
  390. struct rb_irq_work {
  391. struct irq_work work;
  392. wait_queue_head_t waiters;
  393. wait_queue_head_t full_waiters;
  394. atomic_t seq;
  395. bool waiters_pending;
  396. bool full_waiters_pending;
  397. bool wakeup_full;
  398. };
  399. /*
  400. * Structure to hold event state and handle nested events.
  401. */
  402. struct rb_event_info {
  403. u64 ts;
  404. u64 delta;
  405. u64 before;
  406. u64 after;
  407. unsigned long length;
  408. struct buffer_page *tail_page;
  409. int add_timestamp;
  410. };
  411. /*
  412. * Used for the add_timestamp
  413. * NONE
  414. * EXTEND - wants a time extend
  415. * ABSOLUTE - the buffer requests all events to have absolute time stamps
  416. * FORCE - force a full time stamp.
  417. */
  418. enum {
  419. RB_ADD_STAMP_NONE = 0,
  420. RB_ADD_STAMP_EXTEND = BIT(1),
  421. RB_ADD_STAMP_ABSOLUTE = BIT(2),
  422. RB_ADD_STAMP_FORCE = BIT(3)
  423. };
  424. /*
  425. * Used for which event context the event is in.
  426. * TRANSITION = 0
  427. * NMI = 1
  428. * IRQ = 2
  429. * SOFTIRQ = 3
  430. * NORMAL = 4
  431. *
  432. * See trace_recursive_lock() comment below for more details.
  433. */
  434. enum {
  435. RB_CTX_TRANSITION,
  436. RB_CTX_NMI,
  437. RB_CTX_IRQ,
  438. RB_CTX_SOFTIRQ,
  439. RB_CTX_NORMAL,
  440. RB_CTX_MAX
  441. };
  442. struct rb_time_struct {
  443. local64_t time;
  444. };
  445. typedef struct rb_time_struct rb_time_t;
  446. #define MAX_NEST 5
  447. /*
  448. * head_page == tail_page && head == tail then buffer is empty.
  449. */
  450. struct ring_buffer_per_cpu {
  451. int cpu;
  452. atomic_t record_disabled;
  453. atomic_t resize_disabled;
  454. struct trace_buffer *buffer;
  455. raw_spinlock_t reader_lock; /* serialize readers */
  456. arch_spinlock_t lock;
  457. struct lock_class_key lock_key;
  458. struct buffer_data_page *free_page;
  459. unsigned long nr_pages;
  460. unsigned int current_context;
  461. struct list_head *pages;
  462. /* pages generation counter, incremented when the list changes */
  463. unsigned long cnt;
  464. struct buffer_page *head_page; /* read from head */
  465. struct buffer_page *tail_page; /* write to tail */
  466. struct buffer_page *commit_page; /* committed pages */
  467. struct buffer_page *reader_page;
  468. unsigned long lost_events;
  469. unsigned long last_overrun;
  470. unsigned long nest;
  471. local_t entries_bytes;
  472. local_t entries;
  473. local_t overrun;
  474. local_t commit_overrun;
  475. local_t dropped_events;
  476. local_t committing;
  477. local_t commits;
  478. local_t pages_touched;
  479. local_t pages_lost;
  480. local_t pages_read;
  481. long last_pages_touch;
  482. size_t shortest_full;
  483. unsigned long read;
  484. unsigned long read_bytes;
  485. rb_time_t write_stamp;
  486. rb_time_t before_stamp;
  487. u64 event_stamp[MAX_NEST];
  488. u64 read_stamp;
  489. /* pages removed since last reset */
  490. unsigned long pages_removed;
  491. unsigned int mapped;
  492. unsigned int user_mapped; /* user space mapping */
  493. struct mutex mapping_lock;
  494. unsigned long *subbuf_ids; /* ID to subbuf VA */
  495. struct trace_buffer_meta *meta_page;
  496. struct ring_buffer_cpu_meta *ring_meta;
  497. /* ring buffer pages to update, > 0 to add, < 0 to remove */
  498. long nr_pages_to_update;
  499. struct list_head new_pages; /* new pages to add */
  500. struct work_struct update_pages_work;
  501. struct completion update_done;
  502. struct rb_irq_work irq_work;
  503. };
  504. struct trace_buffer {
  505. unsigned flags;
  506. int cpus;
  507. atomic_t record_disabled;
  508. atomic_t resizing;
  509. cpumask_var_t cpumask;
  510. struct lock_class_key *reader_lock_key;
  511. struct mutex mutex;
  512. struct ring_buffer_per_cpu **buffers;
  513. struct hlist_node node;
  514. u64 (*clock)(void);
  515. struct rb_irq_work irq_work;
  516. bool time_stamp_abs;
  517. unsigned long range_addr_start;
  518. unsigned long range_addr_end;
  519. struct ring_buffer_meta *meta;
  520. unsigned int subbuf_size;
  521. unsigned int subbuf_order;
  522. unsigned int max_data_size;
  523. };
  524. struct ring_buffer_iter {
  525. struct ring_buffer_per_cpu *cpu_buffer;
  526. unsigned long head;
  527. unsigned long next_event;
  528. struct buffer_page *head_page;
  529. struct buffer_page *cache_reader_page;
  530. unsigned long cache_read;
  531. unsigned long cache_pages_removed;
  532. u64 read_stamp;
  533. u64 page_stamp;
  534. struct ring_buffer_event *event;
  535. size_t event_size;
  536. int missed_events;
  537. };
  538. int ring_buffer_print_page_header(struct trace_buffer *buffer, struct trace_seq *s)
  539. {
  540. struct buffer_data_page field;
  541. trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  542. "offset:0;\tsize:%u;\tsigned:%u;\n",
  543. (unsigned int)sizeof(field.time_stamp),
  544. (unsigned int)is_signed_type(u64));
  545. trace_seq_printf(s, "\tfield: local_t commit;\t"
  546. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  547. (unsigned int)offsetof(typeof(field), commit),
  548. (unsigned int)sizeof(field.commit),
  549. (unsigned int)is_signed_type(long));
  550. trace_seq_printf(s, "\tfield: int overwrite;\t"
  551. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  552. (unsigned int)offsetof(typeof(field), commit),
  553. 1,
  554. (unsigned int)is_signed_type(long));
  555. trace_seq_printf(s, "\tfield: char data;\t"
  556. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  557. (unsigned int)offsetof(typeof(field), data),
  558. (unsigned int)buffer->subbuf_size,
  559. (unsigned int)is_signed_type(char));
  560. return !trace_seq_has_overflowed(s);
  561. }
  562. static inline void rb_time_read(rb_time_t *t, u64 *ret)
  563. {
  564. *ret = local64_read(&t->time);
  565. }
  566. static void rb_time_set(rb_time_t *t, u64 val)
  567. {
  568. local64_set(&t->time, val);
  569. }
  570. /*
  571. * Enable this to make sure that the event passed to
  572. * ring_buffer_event_time_stamp() is not committed and also
  573. * is on the buffer that it passed in.
  574. */
  575. //#define RB_VERIFY_EVENT
  576. #ifdef RB_VERIFY_EVENT
  577. static struct list_head *rb_list_head(struct list_head *list);
  578. static void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
  579. void *event)
  580. {
  581. struct buffer_page *page = cpu_buffer->commit_page;
  582. struct buffer_page *tail_page = READ_ONCE(cpu_buffer->tail_page);
  583. struct list_head *next;
  584. long commit, write;
  585. unsigned long addr = (unsigned long)event;
  586. bool done = false;
  587. int stop = 0;
  588. /* Make sure the event exists and is not committed yet */
  589. do {
  590. if (page == tail_page || WARN_ON_ONCE(stop++ > 100))
  591. done = true;
  592. commit = local_read(&page->page->commit);
  593. write = local_read(&page->write);
  594. if (addr >= (unsigned long)&page->page->data[commit] &&
  595. addr < (unsigned long)&page->page->data[write])
  596. return;
  597. next = rb_list_head(page->list.next);
  598. page = list_entry(next, struct buffer_page, list);
  599. } while (!done);
  600. WARN_ON_ONCE(1);
  601. }
  602. #else
  603. static inline void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
  604. void *event)
  605. {
  606. }
  607. #endif
  608. /*
  609. * The absolute time stamp drops the 5 MSBs and some clocks may
  610. * require them. The rb_fix_abs_ts() will take a previous full
  611. * time stamp, and add the 5 MSB of that time stamp on to the
  612. * saved absolute time stamp. Then they are compared in case of
  613. * the unlikely event that the latest time stamp incremented
  614. * the 5 MSB.
  615. */
  616. static inline u64 rb_fix_abs_ts(u64 abs, u64 save_ts)
  617. {
  618. if (save_ts & TS_MSB) {
  619. abs |= save_ts & TS_MSB;
  620. /* Check for overflow */
  621. if (unlikely(abs < save_ts))
  622. abs += 1ULL << 59;
  623. }
  624. return abs;
  625. }
  626. static inline u64 rb_time_stamp(struct trace_buffer *buffer);
  627. /**
  628. * ring_buffer_event_time_stamp - return the event's current time stamp
  629. * @buffer: The buffer that the event is on
  630. * @event: the event to get the time stamp of
  631. *
  632. * Note, this must be called after @event is reserved, and before it is
  633. * committed to the ring buffer. And must be called from the same
  634. * context where the event was reserved (normal, softirq, irq, etc).
  635. *
  636. * Returns the time stamp associated with the current event.
  637. * If the event has an extended time stamp, then that is used as
  638. * the time stamp to return.
  639. * In the highly unlikely case that the event was nested more than
  640. * the max nesting, then the write_stamp of the buffer is returned,
  641. * otherwise current time is returned, but that really neither of
  642. * the last two cases should ever happen.
  643. */
  644. u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,
  645. struct ring_buffer_event *event)
  646. {
  647. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()];
  648. unsigned int nest;
  649. u64 ts;
  650. /* If the event includes an absolute time, then just use that */
  651. if (event->type_len == RINGBUF_TYPE_TIME_STAMP) {
  652. ts = rb_event_time_stamp(event);
  653. return rb_fix_abs_ts(ts, cpu_buffer->tail_page->page->time_stamp);
  654. }
  655. nest = local_read(&cpu_buffer->committing);
  656. verify_event(cpu_buffer, event);
  657. if (WARN_ON_ONCE(!nest))
  658. goto fail;
  659. /* Read the current saved nesting level time stamp */
  660. if (likely(--nest < MAX_NEST))
  661. return cpu_buffer->event_stamp[nest];
  662. /* Shouldn't happen, warn if it does */
  663. WARN_ONCE(1, "nest (%d) greater than max", nest);
  664. fail:
  665. rb_time_read(&cpu_buffer->write_stamp, &ts);
  666. return ts;
  667. }
  668. /**
  669. * ring_buffer_nr_dirty_pages - get the number of used pages in the ring buffer
  670. * @buffer: The ring_buffer to get the number of pages from
  671. * @cpu: The cpu of the ring_buffer to get the number of pages from
  672. *
  673. * Returns the number of pages that have content in the ring buffer.
  674. */
  675. size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
  676. {
  677. size_t read;
  678. size_t lost;
  679. size_t cnt;
  680. read = local_read(&buffer->buffers[cpu]->pages_read);
  681. lost = local_read(&buffer->buffers[cpu]->pages_lost);
  682. cnt = local_read(&buffer->buffers[cpu]->pages_touched);
  683. if (WARN_ON_ONCE(cnt < lost))
  684. return 0;
  685. cnt -= lost;
  686. /* The reader can read an empty page, but not more than that */
  687. if (cnt < read) {
  688. WARN_ON_ONCE(read > cnt + 1);
  689. return 0;
  690. }
  691. return cnt - read;
  692. }
  693. static __always_inline bool full_hit(struct trace_buffer *buffer, int cpu, int full)
  694. {
  695. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  696. size_t nr_pages;
  697. size_t dirty;
  698. nr_pages = cpu_buffer->nr_pages;
  699. if (!nr_pages || !full)
  700. return true;
  701. /*
  702. * Add one as dirty will never equal nr_pages, as the sub-buffer
  703. * that the writer is on is not counted as dirty.
  704. * This is needed if "buffer_percent" is set to 100.
  705. */
  706. dirty = ring_buffer_nr_dirty_pages(buffer, cpu) + 1;
  707. return (dirty * 100) >= (full * nr_pages);
  708. }
  709. /*
  710. * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
  711. *
  712. * Schedules a delayed work to wake up any task that is blocked on the
  713. * ring buffer waiters queue.
  714. */
  715. static void rb_wake_up_waiters(struct irq_work *work)
  716. {
  717. struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
  718. /* For waiters waiting for the first wake up */
  719. (void)atomic_fetch_inc_release(&rbwork->seq);
  720. wake_up_all(&rbwork->waiters);
  721. if (rbwork->full_waiters_pending || rbwork->wakeup_full) {
  722. /* Only cpu_buffer sets the above flags */
  723. struct ring_buffer_per_cpu *cpu_buffer =
  724. container_of(rbwork, struct ring_buffer_per_cpu, irq_work);
  725. /* Called from interrupt context */
  726. raw_spin_lock(&cpu_buffer->reader_lock);
  727. rbwork->wakeup_full = false;
  728. rbwork->full_waiters_pending = false;
  729. /* Waking up all waiters, they will reset the shortest full */
  730. cpu_buffer->shortest_full = 0;
  731. raw_spin_unlock(&cpu_buffer->reader_lock);
  732. wake_up_all(&rbwork->full_waiters);
  733. }
  734. }
  735. /**
  736. * ring_buffer_wake_waiters - wake up any waiters on this ring buffer
  737. * @buffer: The ring buffer to wake waiters on
  738. * @cpu: The CPU buffer to wake waiters on
  739. *
  740. * In the case of a file that represents a ring buffer is closing,
  741. * it is prudent to wake up any waiters that are on this.
  742. */
  743. void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu)
  744. {
  745. struct ring_buffer_per_cpu *cpu_buffer;
  746. struct rb_irq_work *rbwork;
  747. if (!buffer)
  748. return;
  749. if (cpu == RING_BUFFER_ALL_CPUS) {
  750. /* Wake up individual ones too. One level recursion */
  751. for_each_buffer_cpu(buffer, cpu)
  752. ring_buffer_wake_waiters(buffer, cpu);
  753. rbwork = &buffer->irq_work;
  754. } else {
  755. if (WARN_ON_ONCE(!buffer->buffers))
  756. return;
  757. if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
  758. return;
  759. cpu_buffer = buffer->buffers[cpu];
  760. /* The CPU buffer may not have been initialized yet */
  761. if (!cpu_buffer)
  762. return;
  763. rbwork = &cpu_buffer->irq_work;
  764. }
  765. /* This can be called in any context */
  766. irq_work_queue(&rbwork->work);
  767. }
  768. static bool rb_watermark_hit(struct trace_buffer *buffer, int cpu, int full)
  769. {
  770. struct ring_buffer_per_cpu *cpu_buffer;
  771. bool ret = false;
  772. /* Reads of all CPUs always waits for any data */
  773. if (cpu == RING_BUFFER_ALL_CPUS)
  774. return !ring_buffer_empty(buffer);
  775. cpu_buffer = buffer->buffers[cpu];
  776. if (!ring_buffer_empty_cpu(buffer, cpu)) {
  777. unsigned long flags;
  778. bool pagebusy;
  779. if (!full)
  780. return true;
  781. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  782. pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
  783. ret = !pagebusy && full_hit(buffer, cpu, full);
  784. if (!ret && (!cpu_buffer->shortest_full ||
  785. cpu_buffer->shortest_full > full)) {
  786. cpu_buffer->shortest_full = full;
  787. }
  788. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  789. }
  790. return ret;
  791. }
  792. static inline bool
  793. rb_wait_cond(struct rb_irq_work *rbwork, struct trace_buffer *buffer,
  794. int cpu, int full, ring_buffer_cond_fn cond, void *data)
  795. {
  796. if (rb_watermark_hit(buffer, cpu, full))
  797. return true;
  798. if (cond(data))
  799. return true;
  800. /*
  801. * The events can happen in critical sections where
  802. * checking a work queue can cause deadlocks.
  803. * After adding a task to the queue, this flag is set
  804. * only to notify events to try to wake up the queue
  805. * using irq_work.
  806. *
  807. * We don't clear it even if the buffer is no longer
  808. * empty. The flag only causes the next event to run
  809. * irq_work to do the work queue wake up. The worse
  810. * that can happen if we race with !trace_empty() is that
  811. * an event will cause an irq_work to try to wake up
  812. * an empty queue.
  813. *
  814. * There's no reason to protect this flag either, as
  815. * the work queue and irq_work logic will do the necessary
  816. * synchronization for the wake ups. The only thing
  817. * that is necessary is that the wake up happens after
  818. * a task has been queued. It's OK for spurious wake ups.
  819. */
  820. if (full)
  821. rbwork->full_waiters_pending = true;
  822. else
  823. rbwork->waiters_pending = true;
  824. return false;
  825. }
  826. struct rb_wait_data {
  827. struct rb_irq_work *irq_work;
  828. int seq;
  829. };
  830. /*
  831. * The default wait condition for ring_buffer_wait() is to just to exit the
  832. * wait loop the first time it is woken up.
  833. */
  834. static bool rb_wait_once(void *data)
  835. {
  836. struct rb_wait_data *rdata = data;
  837. struct rb_irq_work *rbwork = rdata->irq_work;
  838. return atomic_read_acquire(&rbwork->seq) != rdata->seq;
  839. }
  840. /**
  841. * ring_buffer_wait - wait for input to the ring buffer
  842. * @buffer: buffer to wait on
  843. * @cpu: the cpu buffer to wait on
  844. * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
  845. * @cond: condition function to break out of wait (NULL to run once)
  846. * @data: the data to pass to @cond.
  847. *
  848. * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
  849. * as data is added to any of the @buffer's cpu buffers. Otherwise
  850. * it will wait for data to be added to a specific cpu buffer.
  851. */
  852. int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full,
  853. ring_buffer_cond_fn cond, void *data)
  854. {
  855. struct ring_buffer_per_cpu *cpu_buffer;
  856. struct wait_queue_head *waitq;
  857. struct rb_irq_work *rbwork;
  858. struct rb_wait_data rdata;
  859. int ret = 0;
  860. /*
  861. * Depending on what the caller is waiting for, either any
  862. * data in any cpu buffer, or a specific buffer, put the
  863. * caller on the appropriate wait queue.
  864. */
  865. if (cpu == RING_BUFFER_ALL_CPUS) {
  866. rbwork = &buffer->irq_work;
  867. /* Full only makes sense on per cpu reads */
  868. full = 0;
  869. } else {
  870. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  871. return -ENODEV;
  872. cpu_buffer = buffer->buffers[cpu];
  873. rbwork = &cpu_buffer->irq_work;
  874. }
  875. if (full)
  876. waitq = &rbwork->full_waiters;
  877. else
  878. waitq = &rbwork->waiters;
  879. /* Set up to exit loop as soon as it is woken */
  880. if (!cond) {
  881. cond = rb_wait_once;
  882. rdata.irq_work = rbwork;
  883. rdata.seq = atomic_read_acquire(&rbwork->seq);
  884. data = &rdata;
  885. }
  886. ret = wait_event_interruptible((*waitq),
  887. rb_wait_cond(rbwork, buffer, cpu, full, cond, data));
  888. return ret;
  889. }
  890. /**
  891. * ring_buffer_poll_wait - poll on buffer input
  892. * @buffer: buffer to wait on
  893. * @cpu: the cpu buffer to wait on
  894. * @filp: the file descriptor
  895. * @poll_table: The poll descriptor
  896. * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
  897. *
  898. * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
  899. * as data is added to any of the @buffer's cpu buffers. Otherwise
  900. * it will wait for data to be added to a specific cpu buffer.
  901. *
  902. * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
  903. * zero otherwise.
  904. */
  905. __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
  906. struct file *filp, poll_table *poll_table, int full)
  907. {
  908. struct ring_buffer_per_cpu *cpu_buffer;
  909. struct rb_irq_work *rbwork;
  910. if (cpu == RING_BUFFER_ALL_CPUS) {
  911. rbwork = &buffer->irq_work;
  912. full = 0;
  913. } else {
  914. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  915. return EPOLLERR;
  916. cpu_buffer = buffer->buffers[cpu];
  917. rbwork = &cpu_buffer->irq_work;
  918. }
  919. if (full) {
  920. poll_wait(filp, &rbwork->full_waiters, poll_table);
  921. if (rb_watermark_hit(buffer, cpu, full))
  922. return EPOLLIN | EPOLLRDNORM;
  923. /*
  924. * Only allow full_waiters_pending update to be seen after
  925. * the shortest_full is set (in rb_watermark_hit). If the
  926. * writer sees the full_waiters_pending flag set, it will
  927. * compare the amount in the ring buffer to shortest_full.
  928. * If the amount in the ring buffer is greater than the
  929. * shortest_full percent, it will call the irq_work handler
  930. * to wake up this list. The irq_handler will reset shortest_full
  931. * back to zero. That's done under the reader_lock, but
  932. * the below smp_mb() makes sure that the update to
  933. * full_waiters_pending doesn't leak up into the above.
  934. */
  935. smp_mb();
  936. rbwork->full_waiters_pending = true;
  937. return 0;
  938. }
  939. poll_wait(filp, &rbwork->waiters, poll_table);
  940. rbwork->waiters_pending = true;
  941. /*
  942. * There's a tight race between setting the waiters_pending and
  943. * checking if the ring buffer is empty. Once the waiters_pending bit
  944. * is set, the next event will wake the task up, but we can get stuck
  945. * if there's only a single event in.
  946. *
  947. * FIXME: Ideally, we need a memory barrier on the writer side as well,
  948. * but adding a memory barrier to all events will cause too much of a
  949. * performance hit in the fast path. We only need a memory barrier when
  950. * the buffer goes from empty to having content. But as this race is
  951. * extremely small, and it's not a problem if another event comes in, we
  952. * will fix it later.
  953. */
  954. smp_mb();
  955. if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
  956. (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
  957. return EPOLLIN | EPOLLRDNORM;
  958. return 0;
  959. }
  960. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  961. #define RB_WARN_ON(b, cond) \
  962. ({ \
  963. int _____ret = unlikely(cond); \
  964. if (_____ret) { \
  965. if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
  966. struct ring_buffer_per_cpu *__b = \
  967. (void *)b; \
  968. atomic_inc(&__b->buffer->record_disabled); \
  969. } else \
  970. atomic_inc(&b->record_disabled); \
  971. WARN_ON(1); \
  972. } \
  973. _____ret; \
  974. })
  975. /* Up this if you want to test the TIME_EXTENTS and normalization */
  976. #define DEBUG_SHIFT 0
  977. static inline u64 rb_time_stamp(struct trace_buffer *buffer)
  978. {
  979. u64 ts;
  980. /* Skip retpolines :-( */
  981. if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && likely(buffer->clock == trace_clock_local))
  982. ts = trace_clock_local();
  983. else
  984. ts = buffer->clock();
  985. /* shift to debug/test normalization and TIME_EXTENTS */
  986. return ts << DEBUG_SHIFT;
  987. }
  988. u64 ring_buffer_time_stamp(struct trace_buffer *buffer)
  989. {
  990. u64 time;
  991. preempt_disable_notrace();
  992. time = rb_time_stamp(buffer);
  993. preempt_enable_notrace();
  994. return time;
  995. }
  996. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  997. void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
  998. int cpu, u64 *ts)
  999. {
  1000. /* Just stupid testing the normalize function and deltas */
  1001. *ts >>= DEBUG_SHIFT;
  1002. }
  1003. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  1004. /*
  1005. * Making the ring buffer lockless makes things tricky.
  1006. * Although writes only happen on the CPU that they are on,
  1007. * and they only need to worry about interrupts. Reads can
  1008. * happen on any CPU.
  1009. *
  1010. * The reader page is always off the ring buffer, but when the
  1011. * reader finishes with a page, it needs to swap its page with
  1012. * a new one from the buffer. The reader needs to take from
  1013. * the head (writes go to the tail). But if a writer is in overwrite
  1014. * mode and wraps, it must push the head page forward.
  1015. *
  1016. * Here lies the problem.
  1017. *
  1018. * The reader must be careful to replace only the head page, and
  1019. * not another one. As described at the top of the file in the
  1020. * ASCII art, the reader sets its old page to point to the next
  1021. * page after head. It then sets the page after head to point to
  1022. * the old reader page. But if the writer moves the head page
  1023. * during this operation, the reader could end up with the tail.
  1024. *
  1025. * We use cmpxchg to help prevent this race. We also do something
  1026. * special with the page before head. We set the LSB to 1.
  1027. *
  1028. * When the writer must push the page forward, it will clear the
  1029. * bit that points to the head page, move the head, and then set
  1030. * the bit that points to the new head page.
  1031. *
  1032. * We also don't want an interrupt coming in and moving the head
  1033. * page on another writer. Thus we use the second LSB to catch
  1034. * that too. Thus:
  1035. *
  1036. * head->list->prev->next bit 1 bit 0
  1037. * ------- -------
  1038. * Normal page 0 0
  1039. * Points to head page 0 1
  1040. * New head page 1 0
  1041. *
  1042. * Note we can not trust the prev pointer of the head page, because:
  1043. *
  1044. * +----+ +-----+ +-----+
  1045. * | |------>| T |---X--->| N |
  1046. * | |<------| | | |
  1047. * +----+ +-----+ +-----+
  1048. * ^ ^ |
  1049. * | +-----+ | |
  1050. * +----------| R |----------+ |
  1051. * | |<-----------+
  1052. * +-----+
  1053. *
  1054. * Key: ---X--> HEAD flag set in pointer
  1055. * T Tail page
  1056. * R Reader page
  1057. * N Next page
  1058. *
  1059. * (see __rb_reserve_next() to see where this happens)
  1060. *
  1061. * What the above shows is that the reader just swapped out
  1062. * the reader page with a page in the buffer, but before it
  1063. * could make the new header point back to the new page added
  1064. * it was preempted by a writer. The writer moved forward onto
  1065. * the new page added by the reader and is about to move forward
  1066. * again.
  1067. *
  1068. * You can see, it is legitimate for the previous pointer of
  1069. * the head (or any page) not to point back to itself. But only
  1070. * temporarily.
  1071. */
  1072. #define RB_PAGE_NORMAL 0UL
  1073. #define RB_PAGE_HEAD 1UL
  1074. #define RB_PAGE_UPDATE 2UL
  1075. #define RB_FLAG_MASK 3UL
  1076. /* PAGE_MOVED is not part of the mask */
  1077. #define RB_PAGE_MOVED 4UL
  1078. /*
  1079. * rb_list_head - remove any bit
  1080. */
  1081. static struct list_head *rb_list_head(struct list_head *list)
  1082. {
  1083. unsigned long val = (unsigned long)list;
  1084. return (struct list_head *)(val & ~RB_FLAG_MASK);
  1085. }
  1086. /*
  1087. * rb_is_head_page - test if the given page is the head page
  1088. *
  1089. * Because the reader may move the head_page pointer, we can
  1090. * not trust what the head page is (it may be pointing to
  1091. * the reader page). But if the next page is a header page,
  1092. * its flags will be non zero.
  1093. */
  1094. static inline int
  1095. rb_is_head_page(struct buffer_page *page, struct list_head *list)
  1096. {
  1097. unsigned long val;
  1098. val = (unsigned long)list->next;
  1099. if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
  1100. return RB_PAGE_MOVED;
  1101. return val & RB_FLAG_MASK;
  1102. }
  1103. /*
  1104. * rb_is_reader_page
  1105. *
  1106. * The unique thing about the reader page, is that, if the
  1107. * writer is ever on it, the previous pointer never points
  1108. * back to the reader page.
  1109. */
  1110. static bool rb_is_reader_page(struct buffer_page *page)
  1111. {
  1112. struct list_head *list = page->list.prev;
  1113. return rb_list_head(list->next) != &page->list;
  1114. }
  1115. /*
  1116. * rb_set_list_to_head - set a list_head to be pointing to head.
  1117. */
  1118. static void rb_set_list_to_head(struct list_head *list)
  1119. {
  1120. unsigned long *ptr;
  1121. ptr = (unsigned long *)&list->next;
  1122. *ptr |= RB_PAGE_HEAD;
  1123. *ptr &= ~RB_PAGE_UPDATE;
  1124. }
  1125. /*
  1126. * rb_head_page_activate - sets up head page
  1127. */
  1128. static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
  1129. {
  1130. struct buffer_page *head;
  1131. head = cpu_buffer->head_page;
  1132. if (!head)
  1133. return;
  1134. /*
  1135. * Set the previous list pointer to have the HEAD flag.
  1136. */
  1137. rb_set_list_to_head(head->list.prev);
  1138. if (cpu_buffer->ring_meta) {
  1139. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  1140. meta->head_buffer = (unsigned long)head->page;
  1141. }
  1142. }
  1143. static void rb_list_head_clear(struct list_head *list)
  1144. {
  1145. unsigned long *ptr = (unsigned long *)&list->next;
  1146. *ptr &= ~RB_FLAG_MASK;
  1147. }
  1148. /*
  1149. * rb_head_page_deactivate - clears head page ptr (for free list)
  1150. */
  1151. static void
  1152. rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
  1153. {
  1154. struct list_head *hd;
  1155. /* Go through the whole list and clear any pointers found. */
  1156. rb_list_head_clear(cpu_buffer->pages);
  1157. list_for_each(hd, cpu_buffer->pages)
  1158. rb_list_head_clear(hd);
  1159. }
  1160. static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
  1161. struct buffer_page *head,
  1162. struct buffer_page *prev,
  1163. int old_flag, int new_flag)
  1164. {
  1165. struct list_head *list;
  1166. unsigned long val = (unsigned long)&head->list;
  1167. unsigned long ret;
  1168. list = &prev->list;
  1169. val &= ~RB_FLAG_MASK;
  1170. ret = cmpxchg((unsigned long *)&list->next,
  1171. val | old_flag, val | new_flag);
  1172. /* check if the reader took the page */
  1173. if ((ret & ~RB_FLAG_MASK) != val)
  1174. return RB_PAGE_MOVED;
  1175. return ret & RB_FLAG_MASK;
  1176. }
  1177. static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
  1178. struct buffer_page *head,
  1179. struct buffer_page *prev,
  1180. int old_flag)
  1181. {
  1182. return rb_head_page_set(cpu_buffer, head, prev,
  1183. old_flag, RB_PAGE_UPDATE);
  1184. }
  1185. static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
  1186. struct buffer_page *head,
  1187. struct buffer_page *prev,
  1188. int old_flag)
  1189. {
  1190. return rb_head_page_set(cpu_buffer, head, prev,
  1191. old_flag, RB_PAGE_HEAD);
  1192. }
  1193. static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
  1194. struct buffer_page *head,
  1195. struct buffer_page *prev,
  1196. int old_flag)
  1197. {
  1198. return rb_head_page_set(cpu_buffer, head, prev,
  1199. old_flag, RB_PAGE_NORMAL);
  1200. }
  1201. static inline void rb_inc_page(struct buffer_page **bpage)
  1202. {
  1203. struct list_head *p = rb_list_head((*bpage)->list.next);
  1204. *bpage = list_entry(p, struct buffer_page, list);
  1205. }
  1206. static inline void rb_dec_page(struct buffer_page **bpage)
  1207. {
  1208. struct list_head *p = rb_list_head((*bpage)->list.prev);
  1209. *bpage = list_entry(p, struct buffer_page, list);
  1210. }
  1211. static struct buffer_page *
  1212. rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
  1213. {
  1214. struct buffer_page *head;
  1215. struct buffer_page *page;
  1216. struct list_head *list;
  1217. int i;
  1218. if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
  1219. return NULL;
  1220. /* sanity check */
  1221. list = cpu_buffer->pages;
  1222. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
  1223. return NULL;
  1224. page = head = cpu_buffer->head_page;
  1225. /*
  1226. * It is possible that the writer moves the header behind
  1227. * where we started, and we miss in one loop.
  1228. * A second loop should grab the header, but we'll do
  1229. * three loops just because I'm paranoid.
  1230. */
  1231. for (i = 0; i < 3; i++) {
  1232. do {
  1233. if (rb_is_head_page(page, page->list.prev)) {
  1234. cpu_buffer->head_page = page;
  1235. return page;
  1236. }
  1237. rb_inc_page(&page);
  1238. } while (page != head);
  1239. }
  1240. RB_WARN_ON(cpu_buffer, 1);
  1241. return NULL;
  1242. }
  1243. static bool rb_head_page_replace(struct buffer_page *old,
  1244. struct buffer_page *new)
  1245. {
  1246. unsigned long *ptr = (unsigned long *)&old->list.prev->next;
  1247. unsigned long val;
  1248. val = *ptr & ~RB_FLAG_MASK;
  1249. val |= RB_PAGE_HEAD;
  1250. return try_cmpxchg(ptr, &val, (unsigned long)&new->list);
  1251. }
  1252. /*
  1253. * rb_tail_page_update - move the tail page forward
  1254. */
  1255. static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
  1256. struct buffer_page *tail_page,
  1257. struct buffer_page *next_page)
  1258. {
  1259. unsigned long old_entries;
  1260. unsigned long old_write;
  1261. /*
  1262. * The tail page now needs to be moved forward.
  1263. *
  1264. * We need to reset the tail page, but without messing
  1265. * with possible erasing of data brought in by interrupts
  1266. * that have moved the tail page and are currently on it.
  1267. *
  1268. * We add a counter to the write field to denote this.
  1269. */
  1270. old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
  1271. old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
  1272. /*
  1273. * Just make sure we have seen our old_write and synchronize
  1274. * with any interrupts that come in.
  1275. */
  1276. barrier();
  1277. /*
  1278. * If the tail page is still the same as what we think
  1279. * it is, then it is up to us to update the tail
  1280. * pointer.
  1281. */
  1282. if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
  1283. /* Zero the write counter */
  1284. unsigned long val = old_write & ~RB_WRITE_MASK;
  1285. unsigned long eval = old_entries & ~RB_WRITE_MASK;
  1286. /*
  1287. * This will only succeed if an interrupt did
  1288. * not come in and change it. In which case, we
  1289. * do not want to modify it.
  1290. *
  1291. * We add (void) to let the compiler know that we do not care
  1292. * about the return value of these functions. We use the
  1293. * cmpxchg to only update if an interrupt did not already
  1294. * do it for us. If the cmpxchg fails, we don't care.
  1295. */
  1296. (void)local_cmpxchg(&next_page->write, old_write, val);
  1297. (void)local_cmpxchg(&next_page->entries, old_entries, eval);
  1298. /*
  1299. * No need to worry about races with clearing out the commit.
  1300. * it only can increment when a commit takes place. But that
  1301. * only happens in the outer most nested commit.
  1302. */
  1303. local_set(&next_page->page->commit, 0);
  1304. /* Either we update tail_page or an interrupt does */
  1305. if (try_cmpxchg(&cpu_buffer->tail_page, &tail_page, next_page))
  1306. local_inc(&cpu_buffer->pages_touched);
  1307. }
  1308. }
  1309. static void rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
  1310. struct buffer_page *bpage)
  1311. {
  1312. unsigned long val = (unsigned long)bpage;
  1313. RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK);
  1314. }
  1315. static bool rb_check_links(struct ring_buffer_per_cpu *cpu_buffer,
  1316. struct list_head *list)
  1317. {
  1318. if (RB_WARN_ON(cpu_buffer,
  1319. rb_list_head(rb_list_head(list->next)->prev) != list))
  1320. return false;
  1321. if (RB_WARN_ON(cpu_buffer,
  1322. rb_list_head(rb_list_head(list->prev)->next) != list))
  1323. return false;
  1324. return true;
  1325. }
  1326. /**
  1327. * rb_check_pages - integrity check of buffer pages
  1328. * @cpu_buffer: CPU buffer with pages to test
  1329. *
  1330. * As a safety measure we check to make sure the data pages have not
  1331. * been corrupted.
  1332. */
  1333. static void rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  1334. {
  1335. struct list_head *head, *tmp;
  1336. unsigned long buffer_cnt;
  1337. unsigned long flags;
  1338. int nr_loops = 0;
  1339. /*
  1340. * Walk the linked list underpinning the ring buffer and validate all
  1341. * its next and prev links.
  1342. *
  1343. * The check acquires the reader_lock to avoid concurrent processing
  1344. * with code that could be modifying the list. However, the lock cannot
  1345. * be held for the entire duration of the walk, as this would make the
  1346. * time when interrupts are disabled non-deterministic, dependent on the
  1347. * ring buffer size. Therefore, the code releases and re-acquires the
  1348. * lock after checking each page. The ring_buffer_per_cpu.cnt variable
  1349. * is then used to detect if the list was modified while the lock was
  1350. * not held, in which case the check needs to be restarted.
  1351. *
  1352. * The code attempts to perform the check at most three times before
  1353. * giving up. This is acceptable because this is only a self-validation
  1354. * to detect problems early on. In practice, the list modification
  1355. * operations are fairly spaced, and so this check typically succeeds at
  1356. * most on the second try.
  1357. */
  1358. again:
  1359. if (++nr_loops > 3)
  1360. return;
  1361. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1362. head = rb_list_head(cpu_buffer->pages);
  1363. if (!rb_check_links(cpu_buffer, head))
  1364. goto out_locked;
  1365. buffer_cnt = cpu_buffer->cnt;
  1366. tmp = head;
  1367. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1368. while (true) {
  1369. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1370. if (buffer_cnt != cpu_buffer->cnt) {
  1371. /* The list was updated, try again. */
  1372. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1373. goto again;
  1374. }
  1375. tmp = rb_list_head(tmp->next);
  1376. if (tmp == head)
  1377. /* The iteration circled back, all is done. */
  1378. goto out_locked;
  1379. if (!rb_check_links(cpu_buffer, tmp))
  1380. goto out_locked;
  1381. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1382. }
  1383. out_locked:
  1384. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1385. }
  1386. /*
  1387. * Take an address, add the meta data size as well as the array of
  1388. * array subbuffer indexes, then align it to a subbuffer size.
  1389. *
  1390. * This is used to help find the next per cpu subbuffer within a mapped range.
  1391. */
  1392. static unsigned long
  1393. rb_range_align_subbuf(unsigned long addr, int subbuf_size, int nr_subbufs)
  1394. {
  1395. addr += sizeof(struct ring_buffer_cpu_meta) +
  1396. sizeof(int) * nr_subbufs;
  1397. return ALIGN(addr, subbuf_size);
  1398. }
  1399. /*
  1400. * Return the ring_buffer_meta for a given @cpu.
  1401. */
  1402. static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu)
  1403. {
  1404. int subbuf_size = buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
  1405. struct ring_buffer_cpu_meta *meta;
  1406. struct ring_buffer_meta *bmeta;
  1407. unsigned long ptr;
  1408. int nr_subbufs;
  1409. bmeta = buffer->meta;
  1410. if (!bmeta)
  1411. return NULL;
  1412. ptr = (unsigned long)bmeta + bmeta->buffers_offset;
  1413. meta = (struct ring_buffer_cpu_meta *)ptr;
  1414. /* When nr_pages passed in is zero, the first meta has already been initialized */
  1415. if (!nr_pages) {
  1416. nr_subbufs = meta->nr_subbufs;
  1417. } else {
  1418. /* Include the reader page */
  1419. nr_subbufs = nr_pages + 1;
  1420. }
  1421. /*
  1422. * The first chunk may not be subbuffer aligned, where as
  1423. * the rest of the chunks are.
  1424. */
  1425. if (cpu) {
  1426. ptr = rb_range_align_subbuf(ptr, subbuf_size, nr_subbufs);
  1427. ptr += subbuf_size * nr_subbufs;
  1428. /* We can use multiplication to find chunks greater than 1 */
  1429. if (cpu > 1) {
  1430. unsigned long size;
  1431. unsigned long p;
  1432. /* Save the beginning of this CPU chunk */
  1433. p = ptr;
  1434. ptr = rb_range_align_subbuf(ptr, subbuf_size, nr_subbufs);
  1435. ptr += subbuf_size * nr_subbufs;
  1436. /* Now all chunks after this are the same size */
  1437. size = ptr - p;
  1438. ptr += size * (cpu - 2);
  1439. }
  1440. }
  1441. return (void *)ptr;
  1442. }
  1443. /* Return the start of subbufs given the meta pointer */
  1444. static void *rb_subbufs_from_meta(struct ring_buffer_cpu_meta *meta)
  1445. {
  1446. int subbuf_size = meta->subbuf_size;
  1447. unsigned long ptr;
  1448. ptr = (unsigned long)meta;
  1449. ptr = rb_range_align_subbuf(ptr, subbuf_size, meta->nr_subbufs);
  1450. return (void *)ptr;
  1451. }
  1452. /*
  1453. * Return a specific sub-buffer for a given @cpu defined by @idx.
  1454. */
  1455. static void *rb_range_buffer(struct ring_buffer_per_cpu *cpu_buffer, int idx)
  1456. {
  1457. struct ring_buffer_cpu_meta *meta;
  1458. unsigned long ptr;
  1459. int subbuf_size;
  1460. meta = rb_range_meta(cpu_buffer->buffer, 0, cpu_buffer->cpu);
  1461. if (!meta)
  1462. return NULL;
  1463. if (WARN_ON_ONCE(idx >= meta->nr_subbufs))
  1464. return NULL;
  1465. subbuf_size = meta->subbuf_size;
  1466. /* Map this buffer to the order that's in meta->buffers[] */
  1467. idx = meta->buffers[idx];
  1468. ptr = (unsigned long)rb_subbufs_from_meta(meta);
  1469. ptr += subbuf_size * idx;
  1470. if (ptr + subbuf_size > cpu_buffer->buffer->range_addr_end)
  1471. return NULL;
  1472. return (void *)ptr;
  1473. }
  1474. /*
  1475. * See if the existing memory contains a valid meta section.
  1476. * if so, use that, otherwise initialize it.
  1477. */
  1478. static bool rb_meta_init(struct trace_buffer *buffer, int scratch_size)
  1479. {
  1480. unsigned long ptr = buffer->range_addr_start;
  1481. struct ring_buffer_meta *bmeta;
  1482. unsigned long total_size;
  1483. int struct_sizes;
  1484. bmeta = (struct ring_buffer_meta *)ptr;
  1485. buffer->meta = bmeta;
  1486. total_size = buffer->range_addr_end - buffer->range_addr_start;
  1487. struct_sizes = sizeof(struct ring_buffer_cpu_meta);
  1488. struct_sizes |= sizeof(*bmeta) << 16;
  1489. /* The first buffer will start word size after the meta page */
  1490. ptr += sizeof(*bmeta);
  1491. ptr = ALIGN(ptr, sizeof(long));
  1492. ptr += scratch_size;
  1493. if (bmeta->magic != RING_BUFFER_META_MAGIC) {
  1494. pr_info("Ring buffer boot meta mismatch of magic\n");
  1495. goto init;
  1496. }
  1497. if (bmeta->struct_sizes != struct_sizes) {
  1498. pr_info("Ring buffer boot meta mismatch of struct size\n");
  1499. goto init;
  1500. }
  1501. if (bmeta->total_size != total_size) {
  1502. pr_info("Ring buffer boot meta mismatch of total size\n");
  1503. goto init;
  1504. }
  1505. if (bmeta->buffers_offset > bmeta->total_size) {
  1506. pr_info("Ring buffer boot meta mismatch of offset outside of total size\n");
  1507. goto init;
  1508. }
  1509. if (bmeta->buffers_offset != (void *)ptr - (void *)bmeta) {
  1510. pr_info("Ring buffer boot meta mismatch of first buffer offset\n");
  1511. goto init;
  1512. }
  1513. return true;
  1514. init:
  1515. bmeta->magic = RING_BUFFER_META_MAGIC;
  1516. bmeta->struct_sizes = struct_sizes;
  1517. bmeta->total_size = total_size;
  1518. bmeta->buffers_offset = (void *)ptr - (void *)bmeta;
  1519. /* Zero out the scratch pad */
  1520. memset((void *)bmeta + sizeof(*bmeta), 0, bmeta->buffers_offset - sizeof(*bmeta));
  1521. return false;
  1522. }
  1523. /*
  1524. * See if the existing memory contains valid ring buffer data.
  1525. * As the previous kernel must be the same as this kernel, all
  1526. * the calculations (size of buffers and number of buffers)
  1527. * must be the same.
  1528. */
  1529. static bool rb_cpu_meta_valid(struct ring_buffer_cpu_meta *meta, int cpu,
  1530. struct trace_buffer *buffer, int nr_pages,
  1531. unsigned long *subbuf_mask)
  1532. {
  1533. int subbuf_size = PAGE_SIZE;
  1534. struct buffer_data_page *subbuf;
  1535. unsigned long buffers_start;
  1536. unsigned long buffers_end;
  1537. int i;
  1538. if (!subbuf_mask)
  1539. return false;
  1540. buffers_start = meta->first_buffer;
  1541. buffers_end = meta->first_buffer + (subbuf_size * meta->nr_subbufs);
  1542. /* Is the head and commit buffers within the range of buffers? */
  1543. if (meta->head_buffer < buffers_start ||
  1544. meta->head_buffer >= buffers_end) {
  1545. pr_info("Ring buffer boot meta [%d] head buffer out of range\n", cpu);
  1546. return false;
  1547. }
  1548. if (meta->commit_buffer < buffers_start ||
  1549. meta->commit_buffer >= buffers_end) {
  1550. pr_info("Ring buffer boot meta [%d] commit buffer out of range\n", cpu);
  1551. return false;
  1552. }
  1553. subbuf = rb_subbufs_from_meta(meta);
  1554. bitmap_clear(subbuf_mask, 0, meta->nr_subbufs);
  1555. /* Is the meta buffers and the subbufs themselves have correct data? */
  1556. for (i = 0; i < meta->nr_subbufs; i++) {
  1557. if (meta->buffers[i] < 0 ||
  1558. meta->buffers[i] >= meta->nr_subbufs) {
  1559. pr_info("Ring buffer boot meta [%d] array out of range\n", cpu);
  1560. return false;
  1561. }
  1562. if ((unsigned)local_read(&subbuf->commit) > subbuf_size) {
  1563. pr_info("Ring buffer boot meta [%d] buffer invalid commit\n", cpu);
  1564. return false;
  1565. }
  1566. if (test_bit(meta->buffers[i], subbuf_mask)) {
  1567. pr_info("Ring buffer boot meta [%d] array has duplicates\n", cpu);
  1568. return false;
  1569. }
  1570. set_bit(meta->buffers[i], subbuf_mask);
  1571. subbuf = (void *)subbuf + subbuf_size;
  1572. }
  1573. return true;
  1574. }
  1575. static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf);
  1576. static int rb_read_data_buffer(struct buffer_data_page *dpage, int tail, int cpu,
  1577. unsigned long long *timestamp, u64 *delta_ptr)
  1578. {
  1579. struct ring_buffer_event *event;
  1580. u64 ts, delta;
  1581. int events = 0;
  1582. int len;
  1583. int e;
  1584. *delta_ptr = 0;
  1585. *timestamp = 0;
  1586. ts = dpage->time_stamp;
  1587. for (e = 0; e < tail; e += len) {
  1588. event = (struct ring_buffer_event *)(dpage->data + e);
  1589. len = rb_event_length(event);
  1590. if (len <= 0 || len > tail - e)
  1591. return -1;
  1592. switch (event->type_len) {
  1593. case RINGBUF_TYPE_TIME_EXTEND:
  1594. delta = rb_event_time_stamp(event);
  1595. ts += delta;
  1596. break;
  1597. case RINGBUF_TYPE_TIME_STAMP:
  1598. delta = rb_event_time_stamp(event);
  1599. delta = rb_fix_abs_ts(delta, ts);
  1600. if (delta < ts) {
  1601. *delta_ptr = delta;
  1602. *timestamp = ts;
  1603. return -1;
  1604. }
  1605. ts = delta;
  1606. break;
  1607. case RINGBUF_TYPE_PADDING:
  1608. if (event->time_delta == 1)
  1609. break;
  1610. fallthrough;
  1611. case RINGBUF_TYPE_DATA:
  1612. events++;
  1613. ts += event->time_delta;
  1614. break;
  1615. default:
  1616. return -1;
  1617. }
  1618. }
  1619. *timestamp = ts;
  1620. return events;
  1621. }
  1622. static int rb_validate_buffer(struct buffer_data_page *dpage, int cpu)
  1623. {
  1624. unsigned long long ts;
  1625. u64 delta;
  1626. int tail;
  1627. tail = local_read(&dpage->commit);
  1628. return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta);
  1629. }
  1630. /* If the meta data has been validated, now validate the events */
  1631. static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
  1632. {
  1633. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  1634. struct buffer_page *head_page, *orig_head;
  1635. unsigned long entry_bytes = 0;
  1636. unsigned long entries = 0;
  1637. int ret;
  1638. u64 ts;
  1639. int i;
  1640. if (!meta || !meta->head_buffer)
  1641. return;
  1642. orig_head = head_page = cpu_buffer->head_page;
  1643. /* Do the reader page first */
  1644. ret = rb_validate_buffer(cpu_buffer->reader_page->page, cpu_buffer->cpu);
  1645. if (ret < 0) {
  1646. pr_info("Ring buffer reader page is invalid\n");
  1647. goto invalid;
  1648. }
  1649. entries += ret;
  1650. entry_bytes += local_read(&cpu_buffer->reader_page->page->commit);
  1651. local_set(&cpu_buffer->reader_page->entries, ret);
  1652. ts = head_page->page->time_stamp;
  1653. /*
  1654. * Try to rewind the head so that we can read the pages which already
  1655. * read in the previous boot.
  1656. */
  1657. if (head_page == cpu_buffer->tail_page)
  1658. goto skip_rewind;
  1659. rb_dec_page(&head_page);
  1660. for (i = 0; i < meta->nr_subbufs + 1; i++, rb_dec_page(&head_page)) {
  1661. /* Rewind until tail (writer) page. */
  1662. if (head_page == cpu_buffer->tail_page)
  1663. break;
  1664. /* Ensure the page has older data than head. */
  1665. if (ts < head_page->page->time_stamp)
  1666. break;
  1667. ts = head_page->page->time_stamp;
  1668. /* Ensure the page has correct timestamp and some data. */
  1669. if (!ts || rb_page_commit(head_page) == 0)
  1670. break;
  1671. /* Stop rewind if the page is invalid. */
  1672. ret = rb_validate_buffer(head_page->page, cpu_buffer->cpu);
  1673. if (ret < 0)
  1674. break;
  1675. /* Recover the number of entries and update stats. */
  1676. local_set(&head_page->entries, ret);
  1677. if (ret)
  1678. local_inc(&cpu_buffer->pages_touched);
  1679. entries += ret;
  1680. entry_bytes += rb_page_commit(head_page);
  1681. }
  1682. if (i)
  1683. pr_info("Ring buffer [%d] rewound %d pages\n", cpu_buffer->cpu, i);
  1684. /* The last rewound page must be skipped. */
  1685. if (head_page != orig_head)
  1686. rb_inc_page(&head_page);
  1687. /*
  1688. * If the ring buffer was rewound, then inject the reader page
  1689. * into the location just before the original head page.
  1690. */
  1691. if (head_page != orig_head) {
  1692. struct buffer_page *bpage = orig_head;
  1693. rb_dec_page(&bpage);
  1694. /*
  1695. * Insert the reader_page before the original head page.
  1696. * Since the list encode RB_PAGE flags, general list
  1697. * operations should be avoided.
  1698. */
  1699. cpu_buffer->reader_page->list.next = &orig_head->list;
  1700. cpu_buffer->reader_page->list.prev = orig_head->list.prev;
  1701. orig_head->list.prev = &cpu_buffer->reader_page->list;
  1702. bpage->list.next = &cpu_buffer->reader_page->list;
  1703. /* Make the head_page the reader page */
  1704. cpu_buffer->reader_page = head_page;
  1705. bpage = head_page;
  1706. rb_inc_page(&head_page);
  1707. head_page->list.prev = bpage->list.prev;
  1708. rb_dec_page(&bpage);
  1709. bpage->list.next = &head_page->list;
  1710. rb_set_list_to_head(&bpage->list);
  1711. cpu_buffer->pages = &head_page->list;
  1712. cpu_buffer->head_page = head_page;
  1713. meta->head_buffer = (unsigned long)head_page->page;
  1714. /* Reset all the indexes */
  1715. bpage = cpu_buffer->reader_page;
  1716. meta->buffers[0] = rb_meta_subbuf_idx(meta, bpage->page);
  1717. bpage->id = 0;
  1718. for (i = 1, bpage = head_page; i < meta->nr_subbufs;
  1719. i++, rb_inc_page(&bpage)) {
  1720. meta->buffers[i] = rb_meta_subbuf_idx(meta, bpage->page);
  1721. bpage->id = i;
  1722. }
  1723. /* We'll restart verifying from orig_head */
  1724. head_page = orig_head;
  1725. }
  1726. skip_rewind:
  1727. /* If the commit_buffer is the reader page, update the commit page */
  1728. if (meta->commit_buffer == (unsigned long)cpu_buffer->reader_page->page) {
  1729. cpu_buffer->commit_page = cpu_buffer->reader_page;
  1730. /* Nothing more to do, the only page is the reader page */
  1731. goto done;
  1732. }
  1733. /* Iterate until finding the commit page */
  1734. for (i = 0; i < meta->nr_subbufs + 1; i++, rb_inc_page(&head_page)) {
  1735. /* Reader page has already been done */
  1736. if (head_page == cpu_buffer->reader_page)
  1737. continue;
  1738. ret = rb_validate_buffer(head_page->page, cpu_buffer->cpu);
  1739. if (ret < 0) {
  1740. pr_info("Ring buffer meta [%d] invalid buffer page\n",
  1741. cpu_buffer->cpu);
  1742. goto invalid;
  1743. }
  1744. /* If the buffer has content, update pages_touched */
  1745. if (ret)
  1746. local_inc(&cpu_buffer->pages_touched);
  1747. entries += ret;
  1748. entry_bytes += local_read(&head_page->page->commit);
  1749. local_set(&head_page->entries, ret);
  1750. if (head_page == cpu_buffer->commit_page)
  1751. break;
  1752. }
  1753. if (head_page != cpu_buffer->commit_page) {
  1754. pr_info("Ring buffer meta [%d] commit page not found\n",
  1755. cpu_buffer->cpu);
  1756. goto invalid;
  1757. }
  1758. done:
  1759. local_set(&cpu_buffer->entries, entries);
  1760. local_set(&cpu_buffer->entries_bytes, entry_bytes);
  1761. pr_info("Ring buffer meta [%d] is from previous boot!\n", cpu_buffer->cpu);
  1762. return;
  1763. invalid:
  1764. /* The content of the buffers are invalid, reset the meta data */
  1765. meta->head_buffer = 0;
  1766. meta->commit_buffer = 0;
  1767. /* Reset the reader page */
  1768. local_set(&cpu_buffer->reader_page->entries, 0);
  1769. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1770. /* Reset all the subbuffers */
  1771. for (i = 0; i < meta->nr_subbufs - 1; i++, rb_inc_page(&head_page)) {
  1772. local_set(&head_page->entries, 0);
  1773. local_set(&head_page->page->commit, 0);
  1774. }
  1775. }
  1776. static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int scratch_size)
  1777. {
  1778. struct ring_buffer_cpu_meta *meta;
  1779. unsigned long *subbuf_mask;
  1780. unsigned long delta;
  1781. void *subbuf;
  1782. bool valid = false;
  1783. int cpu;
  1784. int i;
  1785. /* Create a mask to test the subbuf array */
  1786. subbuf_mask = bitmap_alloc(nr_pages + 1, GFP_KERNEL);
  1787. /* If subbuf_mask fails to allocate, then rb_meta_valid() will return false */
  1788. if (rb_meta_init(buffer, scratch_size))
  1789. valid = true;
  1790. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  1791. void *next_meta;
  1792. meta = rb_range_meta(buffer, nr_pages, cpu);
  1793. if (valid && rb_cpu_meta_valid(meta, cpu, buffer, nr_pages, subbuf_mask)) {
  1794. /* Make the mappings match the current address */
  1795. subbuf = rb_subbufs_from_meta(meta);
  1796. delta = (unsigned long)subbuf - meta->first_buffer;
  1797. meta->first_buffer += delta;
  1798. meta->head_buffer += delta;
  1799. meta->commit_buffer += delta;
  1800. continue;
  1801. }
  1802. if (cpu < nr_cpu_ids - 1)
  1803. next_meta = rb_range_meta(buffer, nr_pages, cpu + 1);
  1804. else
  1805. next_meta = (void *)buffer->range_addr_end;
  1806. memset(meta, 0, next_meta - (void *)meta);
  1807. meta->nr_subbufs = nr_pages + 1;
  1808. meta->subbuf_size = PAGE_SIZE;
  1809. subbuf = rb_subbufs_from_meta(meta);
  1810. meta->first_buffer = (unsigned long)subbuf;
  1811. /*
  1812. * The buffers[] array holds the order of the sub-buffers
  1813. * that are after the meta data. The sub-buffers may
  1814. * be swapped out when read and inserted into a different
  1815. * location of the ring buffer. Although their addresses
  1816. * remain the same, the buffers[] array contains the
  1817. * index into the sub-buffers holding their actual order.
  1818. */
  1819. for (i = 0; i < meta->nr_subbufs; i++) {
  1820. meta->buffers[i] = i;
  1821. rb_init_page(subbuf);
  1822. subbuf += meta->subbuf_size;
  1823. }
  1824. }
  1825. bitmap_free(subbuf_mask);
  1826. }
  1827. static void *rbm_start(struct seq_file *m, loff_t *pos)
  1828. {
  1829. struct ring_buffer_per_cpu *cpu_buffer = m->private;
  1830. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  1831. unsigned long val;
  1832. if (!meta)
  1833. return NULL;
  1834. if (*pos > meta->nr_subbufs)
  1835. return NULL;
  1836. val = *pos;
  1837. val++;
  1838. return (void *)val;
  1839. }
  1840. static void *rbm_next(struct seq_file *m, void *v, loff_t *pos)
  1841. {
  1842. (*pos)++;
  1843. return rbm_start(m, pos);
  1844. }
  1845. static int rbm_show(struct seq_file *m, void *v)
  1846. {
  1847. struct ring_buffer_per_cpu *cpu_buffer = m->private;
  1848. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  1849. unsigned long val = (unsigned long)v;
  1850. if (val == 1) {
  1851. seq_printf(m, "head_buffer: %d\n",
  1852. rb_meta_subbuf_idx(meta, (void *)meta->head_buffer));
  1853. seq_printf(m, "commit_buffer: %d\n",
  1854. rb_meta_subbuf_idx(meta, (void *)meta->commit_buffer));
  1855. seq_printf(m, "subbuf_size: %d\n", meta->subbuf_size);
  1856. seq_printf(m, "nr_subbufs: %d\n", meta->nr_subbufs);
  1857. return 0;
  1858. }
  1859. val -= 2;
  1860. seq_printf(m, "buffer[%ld]: %d\n", val, meta->buffers[val]);
  1861. return 0;
  1862. }
  1863. static void rbm_stop(struct seq_file *m, void *p)
  1864. {
  1865. }
  1866. static const struct seq_operations rb_meta_seq_ops = {
  1867. .start = rbm_start,
  1868. .next = rbm_next,
  1869. .show = rbm_show,
  1870. .stop = rbm_stop,
  1871. };
  1872. int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, int cpu)
  1873. {
  1874. struct seq_file *m;
  1875. int ret;
  1876. ret = seq_open(file, &rb_meta_seq_ops);
  1877. if (ret)
  1878. return ret;
  1879. m = file->private_data;
  1880. m->private = buffer->buffers[cpu];
  1881. return 0;
  1882. }
  1883. /* Map the buffer_pages to the previous head and commit pages */
  1884. static void rb_meta_buffer_update(struct ring_buffer_per_cpu *cpu_buffer,
  1885. struct buffer_page *bpage)
  1886. {
  1887. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  1888. if (meta->head_buffer == (unsigned long)bpage->page)
  1889. cpu_buffer->head_page = bpage;
  1890. if (meta->commit_buffer == (unsigned long)bpage->page) {
  1891. cpu_buffer->commit_page = bpage;
  1892. cpu_buffer->tail_page = bpage;
  1893. }
  1894. }
  1895. static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1896. long nr_pages, struct list_head *pages)
  1897. {
  1898. struct trace_buffer *buffer = cpu_buffer->buffer;
  1899. struct ring_buffer_cpu_meta *meta = NULL;
  1900. struct buffer_page *bpage, *tmp;
  1901. bool user_thread = current->mm != NULL;
  1902. long i;
  1903. /*
  1904. * Check if the available memory is there first.
  1905. * Note, si_mem_available() only gives us a rough estimate of available
  1906. * memory. It may not be accurate. But we don't care, we just want
  1907. * to prevent doing any allocation when it is obvious that it is
  1908. * not going to succeed.
  1909. */
  1910. i = si_mem_available();
  1911. if (i < nr_pages)
  1912. return -ENOMEM;
  1913. /*
  1914. * If a user thread allocates too much, and si_mem_available()
  1915. * reports there's enough memory, even though there is not.
  1916. * Make sure the OOM killer kills this thread. This can happen
  1917. * even with RETRY_MAYFAIL because another task may be doing
  1918. * an allocation after this task has taken all memory.
  1919. * This is the task the OOM killer needs to take out during this
  1920. * loop, even if it was triggered by an allocation somewhere else.
  1921. */
  1922. if (user_thread)
  1923. set_current_oom_origin();
  1924. if (buffer->range_addr_start)
  1925. meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu);
  1926. for (i = 0; i < nr_pages; i++) {
  1927. bpage = alloc_cpu_page(cpu_buffer->cpu);
  1928. if (!bpage)
  1929. goto free_pages;
  1930. rb_check_bpage(cpu_buffer, bpage);
  1931. /*
  1932. * Append the pages as for mapped buffers we want to keep
  1933. * the order
  1934. */
  1935. list_add_tail(&bpage->list, pages);
  1936. if (meta) {
  1937. /* A range was given. Use that for the buffer page */
  1938. bpage->page = rb_range_buffer(cpu_buffer, i + 1);
  1939. if (!bpage->page)
  1940. goto free_pages;
  1941. /* If this is valid from a previous boot */
  1942. if (meta->head_buffer)
  1943. rb_meta_buffer_update(cpu_buffer, bpage);
  1944. bpage->range = 1;
  1945. bpage->id = i + 1;
  1946. } else {
  1947. int order = cpu_buffer->buffer->subbuf_order;
  1948. bpage->page = alloc_cpu_data(cpu_buffer->cpu, order);
  1949. if (!bpage->page)
  1950. goto free_pages;
  1951. }
  1952. bpage->order = cpu_buffer->buffer->subbuf_order;
  1953. if (user_thread && fatal_signal_pending(current))
  1954. goto free_pages;
  1955. }
  1956. if (user_thread)
  1957. clear_current_oom_origin();
  1958. return 0;
  1959. free_pages:
  1960. list_for_each_entry_safe(bpage, tmp, pages, list) {
  1961. list_del_init(&bpage->list);
  1962. free_buffer_page(bpage);
  1963. }
  1964. if (user_thread)
  1965. clear_current_oom_origin();
  1966. return -ENOMEM;
  1967. }
  1968. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1969. unsigned long nr_pages)
  1970. {
  1971. LIST_HEAD(pages);
  1972. WARN_ON(!nr_pages);
  1973. if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
  1974. return -ENOMEM;
  1975. /*
  1976. * The ring buffer page list is a circular list that does not
  1977. * start and end with a list head. All page list items point to
  1978. * other pages.
  1979. */
  1980. cpu_buffer->pages = pages.next;
  1981. list_del(&pages);
  1982. cpu_buffer->nr_pages = nr_pages;
  1983. rb_check_pages(cpu_buffer);
  1984. return 0;
  1985. }
  1986. static struct ring_buffer_per_cpu *
  1987. rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
  1988. {
  1989. struct ring_buffer_per_cpu *cpu_buffer __free(kfree) =
  1990. alloc_cpu_buffer(cpu);
  1991. struct ring_buffer_cpu_meta *meta;
  1992. struct buffer_page *bpage;
  1993. int ret;
  1994. if (!cpu_buffer)
  1995. return NULL;
  1996. cpu_buffer->cpu = cpu;
  1997. cpu_buffer->buffer = buffer;
  1998. raw_spin_lock_init(&cpu_buffer->reader_lock);
  1999. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  2000. cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  2001. INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
  2002. init_completion(&cpu_buffer->update_done);
  2003. init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
  2004. init_waitqueue_head(&cpu_buffer->irq_work.waiters);
  2005. init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
  2006. mutex_init(&cpu_buffer->mapping_lock);
  2007. bpage = alloc_cpu_page(cpu);
  2008. if (!bpage)
  2009. return NULL;
  2010. rb_check_bpage(cpu_buffer, bpage);
  2011. cpu_buffer->reader_page = bpage;
  2012. if (buffer->range_addr_start) {
  2013. /*
  2014. * Range mapped buffers have the same restrictions as memory
  2015. * mapped ones do.
  2016. */
  2017. cpu_buffer->mapped = 1;
  2018. cpu_buffer->ring_meta = rb_range_meta(buffer, nr_pages, cpu);
  2019. bpage->page = rb_range_buffer(cpu_buffer, 0);
  2020. if (!bpage->page)
  2021. goto fail_free_reader;
  2022. if (cpu_buffer->ring_meta->head_buffer)
  2023. rb_meta_buffer_update(cpu_buffer, bpage);
  2024. bpage->range = 1;
  2025. } else {
  2026. int order = cpu_buffer->buffer->subbuf_order;
  2027. bpage->page = alloc_cpu_data(cpu, order);
  2028. if (!bpage->page)
  2029. goto fail_free_reader;
  2030. }
  2031. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2032. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  2033. ret = rb_allocate_pages(cpu_buffer, nr_pages);
  2034. if (ret < 0)
  2035. goto fail_free_reader;
  2036. rb_meta_validate_events(cpu_buffer);
  2037. /* If the boot meta was valid then this has already been updated */
  2038. meta = cpu_buffer->ring_meta;
  2039. if (!meta || !meta->head_buffer ||
  2040. !cpu_buffer->head_page || !cpu_buffer->commit_page || !cpu_buffer->tail_page) {
  2041. if (meta && meta->head_buffer &&
  2042. (cpu_buffer->head_page || cpu_buffer->commit_page || cpu_buffer->tail_page)) {
  2043. pr_warn("Ring buffer meta buffers not all mapped\n");
  2044. if (!cpu_buffer->head_page)
  2045. pr_warn(" Missing head_page\n");
  2046. if (!cpu_buffer->commit_page)
  2047. pr_warn(" Missing commit_page\n");
  2048. if (!cpu_buffer->tail_page)
  2049. pr_warn(" Missing tail_page\n");
  2050. }
  2051. cpu_buffer->head_page
  2052. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  2053. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  2054. rb_head_page_activate(cpu_buffer);
  2055. if (cpu_buffer->ring_meta)
  2056. meta->commit_buffer = meta->head_buffer;
  2057. } else {
  2058. /* The valid meta buffer still needs to activate the head page */
  2059. rb_head_page_activate(cpu_buffer);
  2060. }
  2061. return_ptr(cpu_buffer);
  2062. fail_free_reader:
  2063. free_buffer_page(cpu_buffer->reader_page);
  2064. return NULL;
  2065. }
  2066. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  2067. {
  2068. struct list_head *head = cpu_buffer->pages;
  2069. struct buffer_page *bpage, *tmp;
  2070. irq_work_sync(&cpu_buffer->irq_work.work);
  2071. free_buffer_page(cpu_buffer->reader_page);
  2072. if (head) {
  2073. rb_head_page_deactivate(cpu_buffer);
  2074. list_for_each_entry_safe(bpage, tmp, head, list) {
  2075. list_del_init(&bpage->list);
  2076. free_buffer_page(bpage);
  2077. }
  2078. bpage = list_entry(head, struct buffer_page, list);
  2079. free_buffer_page(bpage);
  2080. }
  2081. free_page((unsigned long)cpu_buffer->free_page);
  2082. kfree(cpu_buffer);
  2083. }
  2084. static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
  2085. int order, unsigned long start,
  2086. unsigned long end,
  2087. unsigned long scratch_size,
  2088. struct lock_class_key *key)
  2089. {
  2090. struct trace_buffer *buffer __free(kfree) = NULL;
  2091. long nr_pages;
  2092. int subbuf_size;
  2093. int bsize;
  2094. int cpu;
  2095. int ret;
  2096. /* keep it in its own cache line */
  2097. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  2098. GFP_KERNEL);
  2099. if (!buffer)
  2100. return NULL;
  2101. if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  2102. return NULL;
  2103. buffer->subbuf_order = order;
  2104. subbuf_size = (PAGE_SIZE << order);
  2105. buffer->subbuf_size = subbuf_size - BUF_PAGE_HDR_SIZE;
  2106. /* Max payload is buffer page size - header (8bytes) */
  2107. buffer->max_data_size = buffer->subbuf_size - (sizeof(u32) * 2);
  2108. buffer->flags = flags;
  2109. buffer->clock = trace_clock_local;
  2110. buffer->reader_lock_key = key;
  2111. init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
  2112. init_waitqueue_head(&buffer->irq_work.waiters);
  2113. buffer->cpus = nr_cpu_ids;
  2114. bsize = sizeof(void *) * nr_cpu_ids;
  2115. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  2116. GFP_KERNEL);
  2117. if (!buffer->buffers)
  2118. goto fail_free_cpumask;
  2119. /* If start/end are specified, then that overrides size */
  2120. if (start && end) {
  2121. unsigned long buffers_start;
  2122. unsigned long ptr;
  2123. int n;
  2124. /* Make sure that start is word aligned */
  2125. start = ALIGN(start, sizeof(long));
  2126. /* scratch_size needs to be aligned too */
  2127. scratch_size = ALIGN(scratch_size, sizeof(long));
  2128. /* Subtract the buffer meta data and word aligned */
  2129. buffers_start = start + sizeof(struct ring_buffer_cpu_meta);
  2130. buffers_start = ALIGN(buffers_start, sizeof(long));
  2131. buffers_start += scratch_size;
  2132. /* Calculate the size for the per CPU data */
  2133. size = end - buffers_start;
  2134. size = size / nr_cpu_ids;
  2135. /*
  2136. * The number of sub-buffers (nr_pages) is determined by the
  2137. * total size allocated minus the meta data size.
  2138. * Then that is divided by the number of per CPU buffers
  2139. * needed, plus account for the integer array index that
  2140. * will be appended to the meta data.
  2141. */
  2142. nr_pages = (size - sizeof(struct ring_buffer_cpu_meta)) /
  2143. (subbuf_size + sizeof(int));
  2144. /* Need at least two pages plus the reader page */
  2145. if (nr_pages < 3)
  2146. goto fail_free_buffers;
  2147. again:
  2148. /* Make sure that the size fits aligned */
  2149. for (n = 0, ptr = buffers_start; n < nr_cpu_ids; n++) {
  2150. ptr += sizeof(struct ring_buffer_cpu_meta) +
  2151. sizeof(int) * nr_pages;
  2152. ptr = ALIGN(ptr, subbuf_size);
  2153. ptr += subbuf_size * nr_pages;
  2154. }
  2155. if (ptr > end) {
  2156. if (nr_pages <= 3)
  2157. goto fail_free_buffers;
  2158. nr_pages--;
  2159. goto again;
  2160. }
  2161. /* nr_pages should not count the reader page */
  2162. nr_pages--;
  2163. buffer->range_addr_start = start;
  2164. buffer->range_addr_end = end;
  2165. rb_range_meta_init(buffer, nr_pages, scratch_size);
  2166. } else {
  2167. /* need at least two pages */
  2168. nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
  2169. if (nr_pages < 2)
  2170. nr_pages = 2;
  2171. }
  2172. cpu = raw_smp_processor_id();
  2173. cpumask_set_cpu(cpu, buffer->cpumask);
  2174. buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  2175. if (!buffer->buffers[cpu])
  2176. goto fail_free_buffers;
  2177. ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
  2178. if (ret < 0)
  2179. goto fail_free_buffers;
  2180. mutex_init(&buffer->mutex);
  2181. return_ptr(buffer);
  2182. fail_free_buffers:
  2183. for_each_buffer_cpu(buffer, cpu) {
  2184. if (buffer->buffers[cpu])
  2185. rb_free_cpu_buffer(buffer->buffers[cpu]);
  2186. }
  2187. kfree(buffer->buffers);
  2188. fail_free_cpumask:
  2189. free_cpumask_var(buffer->cpumask);
  2190. return NULL;
  2191. }
  2192. /**
  2193. * __ring_buffer_alloc - allocate a new ring_buffer
  2194. * @size: the size in bytes per cpu that is needed.
  2195. * @flags: attributes to set for the ring buffer.
  2196. * @key: ring buffer reader_lock_key.
  2197. *
  2198. * Currently the only flag that is available is the RB_FL_OVERWRITE
  2199. * flag. This flag means that the buffer will overwrite old data
  2200. * when the buffer wraps. If this flag is not set, the buffer will
  2201. * drop data when the tail hits the head.
  2202. */
  2203. struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  2204. struct lock_class_key *key)
  2205. {
  2206. /* Default buffer page size - one system page */
  2207. return alloc_buffer(size, flags, 0, 0, 0, 0, key);
  2208. }
  2209. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  2210. /**
  2211. * __ring_buffer_alloc_range - allocate a new ring_buffer from existing memory
  2212. * @size: the size in bytes per cpu that is needed.
  2213. * @flags: attributes to set for the ring buffer.
  2214. * @order: sub-buffer order
  2215. * @start: start of allocated range
  2216. * @range_size: size of allocated range
  2217. * @scratch_size: size of scratch area (for preallocated memory buffers)
  2218. * @key: ring buffer reader_lock_key.
  2219. *
  2220. * Currently the only flag that is available is the RB_FL_OVERWRITE
  2221. * flag. This flag means that the buffer will overwrite old data
  2222. * when the buffer wraps. If this flag is not set, the buffer will
  2223. * drop data when the tail hits the head.
  2224. */
  2225. struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flags,
  2226. int order, unsigned long start,
  2227. unsigned long range_size,
  2228. unsigned long scratch_size,
  2229. struct lock_class_key *key)
  2230. {
  2231. return alloc_buffer(size, flags, order, start, start + range_size,
  2232. scratch_size, key);
  2233. }
  2234. void *ring_buffer_meta_scratch(struct trace_buffer *buffer, unsigned int *size)
  2235. {
  2236. struct ring_buffer_meta *meta;
  2237. void *ptr;
  2238. if (!buffer || !buffer->meta)
  2239. return NULL;
  2240. meta = buffer->meta;
  2241. ptr = (void *)ALIGN((unsigned long)meta + sizeof(*meta), sizeof(long));
  2242. if (size)
  2243. *size = (void *)meta + meta->buffers_offset - ptr;
  2244. return ptr;
  2245. }
  2246. /**
  2247. * ring_buffer_free - free a ring buffer.
  2248. * @buffer: the buffer to free.
  2249. */
  2250. void
  2251. ring_buffer_free(struct trace_buffer *buffer)
  2252. {
  2253. int cpu;
  2254. cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
  2255. irq_work_sync(&buffer->irq_work.work);
  2256. for_each_buffer_cpu(buffer, cpu)
  2257. rb_free_cpu_buffer(buffer->buffers[cpu]);
  2258. kfree(buffer->buffers);
  2259. free_cpumask_var(buffer->cpumask);
  2260. kfree(buffer);
  2261. }
  2262. EXPORT_SYMBOL_GPL(ring_buffer_free);
  2263. void ring_buffer_set_clock(struct trace_buffer *buffer,
  2264. u64 (*clock)(void))
  2265. {
  2266. buffer->clock = clock;
  2267. }
  2268. void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
  2269. {
  2270. buffer->time_stamp_abs = abs;
  2271. }
  2272. bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
  2273. {
  2274. return buffer->time_stamp_abs;
  2275. }
  2276. static inline unsigned long rb_page_entries(struct buffer_page *bpage)
  2277. {
  2278. return local_read(&bpage->entries) & RB_WRITE_MASK;
  2279. }
  2280. static inline unsigned long rb_page_write(struct buffer_page *bpage)
  2281. {
  2282. return local_read(&bpage->write) & RB_WRITE_MASK;
  2283. }
  2284. static bool
  2285. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
  2286. {
  2287. struct list_head *tail_page, *to_remove, *next_page;
  2288. struct buffer_page *to_remove_page, *tmp_iter_page;
  2289. struct buffer_page *last_page, *first_page;
  2290. unsigned long nr_removed;
  2291. unsigned long head_bit;
  2292. int page_entries;
  2293. head_bit = 0;
  2294. raw_spin_lock_irq(&cpu_buffer->reader_lock);
  2295. atomic_inc(&cpu_buffer->record_disabled);
  2296. /*
  2297. * We don't race with the readers since we have acquired the reader
  2298. * lock. We also don't race with writers after disabling recording.
  2299. * This makes it easy to figure out the first and the last page to be
  2300. * removed from the list. We unlink all the pages in between including
  2301. * the first and last pages. This is done in a busy loop so that we
  2302. * lose the least number of traces.
  2303. * The pages are freed after we restart recording and unlock readers.
  2304. */
  2305. tail_page = &cpu_buffer->tail_page->list;
  2306. /*
  2307. * tail page might be on reader page, we remove the next page
  2308. * from the ring buffer
  2309. */
  2310. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  2311. tail_page = rb_list_head(tail_page->next);
  2312. to_remove = tail_page;
  2313. /* start of pages to remove */
  2314. first_page = list_entry(rb_list_head(to_remove->next),
  2315. struct buffer_page, list);
  2316. for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
  2317. to_remove = rb_list_head(to_remove)->next;
  2318. head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
  2319. }
  2320. /* Read iterators need to reset themselves when some pages removed */
  2321. cpu_buffer->pages_removed += nr_removed;
  2322. next_page = rb_list_head(to_remove)->next;
  2323. /*
  2324. * Now we remove all pages between tail_page and next_page.
  2325. * Make sure that we have head_bit value preserved for the
  2326. * next page
  2327. */
  2328. tail_page->next = (struct list_head *)((unsigned long)next_page |
  2329. head_bit);
  2330. next_page = rb_list_head(next_page);
  2331. next_page->prev = tail_page;
  2332. /* make sure pages points to a valid page in the ring buffer */
  2333. cpu_buffer->pages = next_page;
  2334. cpu_buffer->cnt++;
  2335. /* update head page */
  2336. if (head_bit)
  2337. cpu_buffer->head_page = list_entry(next_page,
  2338. struct buffer_page, list);
  2339. /* pages are removed, resume tracing and then free the pages */
  2340. atomic_dec(&cpu_buffer->record_disabled);
  2341. raw_spin_unlock_irq(&cpu_buffer->reader_lock);
  2342. RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
  2343. /* last buffer page to remove */
  2344. last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
  2345. list);
  2346. tmp_iter_page = first_page;
  2347. do {
  2348. cond_resched();
  2349. to_remove_page = tmp_iter_page;
  2350. rb_inc_page(&tmp_iter_page);
  2351. /* update the counters */
  2352. page_entries = rb_page_entries(to_remove_page);
  2353. if (page_entries) {
  2354. /*
  2355. * If something was added to this page, it was full
  2356. * since it is not the tail page. So we deduct the
  2357. * bytes consumed in ring buffer from here.
  2358. * Increment overrun to account for the lost events.
  2359. */
  2360. local_add(page_entries, &cpu_buffer->overrun);
  2361. local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes);
  2362. local_inc(&cpu_buffer->pages_lost);
  2363. }
  2364. /*
  2365. * We have already removed references to this list item, just
  2366. * free up the buffer_page and its page
  2367. */
  2368. free_buffer_page(to_remove_page);
  2369. nr_removed--;
  2370. } while (to_remove_page != last_page);
  2371. RB_WARN_ON(cpu_buffer, nr_removed);
  2372. return nr_removed == 0;
  2373. }
  2374. static bool
  2375. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
  2376. {
  2377. struct list_head *pages = &cpu_buffer->new_pages;
  2378. unsigned long flags;
  2379. bool success;
  2380. int retries;
  2381. /* Can be called at early boot up, where interrupts must not been enabled */
  2382. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2383. /*
  2384. * We are holding the reader lock, so the reader page won't be swapped
  2385. * in the ring buffer. Now we are racing with the writer trying to
  2386. * move head page and the tail page.
  2387. * We are going to adapt the reader page update process where:
  2388. * 1. We first splice the start and end of list of new pages between
  2389. * the head page and its previous page.
  2390. * 2. We cmpxchg the prev_page->next to point from head page to the
  2391. * start of new pages list.
  2392. * 3. Finally, we update the head->prev to the end of new list.
  2393. *
  2394. * We will try this process 10 times, to make sure that we don't keep
  2395. * spinning.
  2396. */
  2397. retries = 10;
  2398. success = false;
  2399. while (retries--) {
  2400. struct list_head *head_page, *prev_page;
  2401. struct list_head *last_page, *first_page;
  2402. struct list_head *head_page_with_bit;
  2403. struct buffer_page *hpage = rb_set_head_page(cpu_buffer);
  2404. if (!hpage)
  2405. break;
  2406. head_page = &hpage->list;
  2407. prev_page = head_page->prev;
  2408. first_page = pages->next;
  2409. last_page = pages->prev;
  2410. head_page_with_bit = (struct list_head *)
  2411. ((unsigned long)head_page | RB_PAGE_HEAD);
  2412. last_page->next = head_page_with_bit;
  2413. first_page->prev = prev_page;
  2414. /* caution: head_page_with_bit gets updated on cmpxchg failure */
  2415. if (try_cmpxchg(&prev_page->next,
  2416. &head_page_with_bit, first_page)) {
  2417. /*
  2418. * yay, we replaced the page pointer to our new list,
  2419. * now, we just have to update to head page's prev
  2420. * pointer to point to end of list
  2421. */
  2422. head_page->prev = last_page;
  2423. cpu_buffer->cnt++;
  2424. success = true;
  2425. break;
  2426. }
  2427. }
  2428. if (success)
  2429. INIT_LIST_HEAD(pages);
  2430. /*
  2431. * If we weren't successful in adding in new pages, warn and stop
  2432. * tracing
  2433. */
  2434. RB_WARN_ON(cpu_buffer, !success);
  2435. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2436. /* free pages if they weren't inserted */
  2437. if (!success) {
  2438. struct buffer_page *bpage, *tmp;
  2439. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  2440. list) {
  2441. list_del_init(&bpage->list);
  2442. free_buffer_page(bpage);
  2443. }
  2444. }
  2445. return success;
  2446. }
  2447. static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
  2448. {
  2449. bool success;
  2450. if (cpu_buffer->nr_pages_to_update > 0)
  2451. success = rb_insert_pages(cpu_buffer);
  2452. else
  2453. success = rb_remove_pages(cpu_buffer,
  2454. -cpu_buffer->nr_pages_to_update);
  2455. if (success)
  2456. cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
  2457. }
  2458. static void update_pages_handler(struct work_struct *work)
  2459. {
  2460. struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
  2461. struct ring_buffer_per_cpu, update_pages_work);
  2462. rb_update_pages(cpu_buffer);
  2463. complete(&cpu_buffer->update_done);
  2464. }
  2465. /**
  2466. * ring_buffer_resize - resize the ring buffer
  2467. * @buffer: the buffer to resize.
  2468. * @size: the new size.
  2469. * @cpu_id: the cpu buffer to resize
  2470. *
  2471. * Minimum size is 2 * buffer->subbuf_size.
  2472. *
  2473. * Returns 0 on success and < 0 on failure.
  2474. */
  2475. int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
  2476. int cpu_id)
  2477. {
  2478. struct ring_buffer_per_cpu *cpu_buffer;
  2479. unsigned long nr_pages;
  2480. int cpu, err;
  2481. /*
  2482. * Always succeed at resizing a non-existent buffer:
  2483. */
  2484. if (!buffer)
  2485. return 0;
  2486. /* Make sure the requested buffer exists */
  2487. if (cpu_id != RING_BUFFER_ALL_CPUS &&
  2488. !cpumask_test_cpu(cpu_id, buffer->cpumask))
  2489. return 0;
  2490. nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
  2491. /* we need a minimum of two pages */
  2492. if (nr_pages < 2)
  2493. nr_pages = 2;
  2494. /*
  2495. * Keep CPUs from coming online while resizing to synchronize
  2496. * with new per CPU buffers being created.
  2497. */
  2498. guard(cpus_read_lock)();
  2499. /* prevent another thread from changing buffer sizes */
  2500. mutex_lock(&buffer->mutex);
  2501. atomic_inc(&buffer->resizing);
  2502. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  2503. /*
  2504. * Don't succeed if resizing is disabled, as a reader might be
  2505. * manipulating the ring buffer and is expecting a sane state while
  2506. * this is true.
  2507. */
  2508. for_each_buffer_cpu(buffer, cpu) {
  2509. cpu_buffer = buffer->buffers[cpu];
  2510. if (atomic_read(&cpu_buffer->resize_disabled)) {
  2511. err = -EBUSY;
  2512. goto out_err_unlock;
  2513. }
  2514. }
  2515. /* calculate the pages to update */
  2516. for_each_buffer_cpu(buffer, cpu) {
  2517. cpu_buffer = buffer->buffers[cpu];
  2518. cpu_buffer->nr_pages_to_update = nr_pages -
  2519. cpu_buffer->nr_pages;
  2520. /*
  2521. * nothing more to do for removing pages or no update
  2522. */
  2523. if (cpu_buffer->nr_pages_to_update <= 0)
  2524. continue;
  2525. /*
  2526. * to add pages, make sure all new pages can be
  2527. * allocated without receiving ENOMEM
  2528. */
  2529. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  2530. if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
  2531. &cpu_buffer->new_pages)) {
  2532. /* not enough memory for new pages */
  2533. err = -ENOMEM;
  2534. goto out_err;
  2535. }
  2536. cond_resched();
  2537. }
  2538. /*
  2539. * Fire off all the required work handlers
  2540. * We can't schedule on offline CPUs, but it's not necessary
  2541. * since we can change their buffer sizes without any race.
  2542. */
  2543. for_each_buffer_cpu(buffer, cpu) {
  2544. cpu_buffer = buffer->buffers[cpu];
  2545. if (!cpu_buffer->nr_pages_to_update)
  2546. continue;
  2547. /* Can't run something on an offline CPU. */
  2548. if (!cpu_online(cpu)) {
  2549. rb_update_pages(cpu_buffer);
  2550. cpu_buffer->nr_pages_to_update = 0;
  2551. } else {
  2552. /* Run directly if possible. */
  2553. migrate_disable();
  2554. if (cpu != smp_processor_id()) {
  2555. migrate_enable();
  2556. schedule_work_on(cpu,
  2557. &cpu_buffer->update_pages_work);
  2558. } else {
  2559. update_pages_handler(&cpu_buffer->update_pages_work);
  2560. migrate_enable();
  2561. }
  2562. }
  2563. }
  2564. /* wait for all the updates to complete */
  2565. for_each_buffer_cpu(buffer, cpu) {
  2566. cpu_buffer = buffer->buffers[cpu];
  2567. if (!cpu_buffer->nr_pages_to_update)
  2568. continue;
  2569. if (cpu_online(cpu))
  2570. wait_for_completion(&cpu_buffer->update_done);
  2571. cpu_buffer->nr_pages_to_update = 0;
  2572. }
  2573. } else {
  2574. cpu_buffer = buffer->buffers[cpu_id];
  2575. if (nr_pages == cpu_buffer->nr_pages)
  2576. goto out;
  2577. /*
  2578. * Don't succeed if resizing is disabled, as a reader might be
  2579. * manipulating the ring buffer and is expecting a sane state while
  2580. * this is true.
  2581. */
  2582. if (atomic_read(&cpu_buffer->resize_disabled)) {
  2583. err = -EBUSY;
  2584. goto out_err_unlock;
  2585. }
  2586. cpu_buffer->nr_pages_to_update = nr_pages -
  2587. cpu_buffer->nr_pages;
  2588. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  2589. if (cpu_buffer->nr_pages_to_update > 0 &&
  2590. __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
  2591. &cpu_buffer->new_pages)) {
  2592. err = -ENOMEM;
  2593. goto out_err;
  2594. }
  2595. /* Can't run something on an offline CPU. */
  2596. if (!cpu_online(cpu_id))
  2597. rb_update_pages(cpu_buffer);
  2598. else {
  2599. /* Run directly if possible. */
  2600. migrate_disable();
  2601. if (cpu_id == smp_processor_id()) {
  2602. rb_update_pages(cpu_buffer);
  2603. migrate_enable();
  2604. } else {
  2605. migrate_enable();
  2606. schedule_work_on(cpu_id,
  2607. &cpu_buffer->update_pages_work);
  2608. wait_for_completion(&cpu_buffer->update_done);
  2609. }
  2610. }
  2611. cpu_buffer->nr_pages_to_update = 0;
  2612. }
  2613. out:
  2614. /*
  2615. * The ring buffer resize can happen with the ring buffer
  2616. * enabled, so that the update disturbs the tracing as little
  2617. * as possible. But if the buffer is disabled, we do not need
  2618. * to worry about that, and we can take the time to verify
  2619. * that the buffer is not corrupt.
  2620. */
  2621. if (atomic_read(&buffer->record_disabled)) {
  2622. atomic_inc(&buffer->record_disabled);
  2623. /*
  2624. * Even though the buffer was disabled, we must make sure
  2625. * that it is truly disabled before calling rb_check_pages.
  2626. * There could have been a race between checking
  2627. * record_disable and incrementing it.
  2628. */
  2629. synchronize_rcu();
  2630. for_each_buffer_cpu(buffer, cpu) {
  2631. cpu_buffer = buffer->buffers[cpu];
  2632. rb_check_pages(cpu_buffer);
  2633. }
  2634. atomic_dec(&buffer->record_disabled);
  2635. }
  2636. atomic_dec(&buffer->resizing);
  2637. mutex_unlock(&buffer->mutex);
  2638. return 0;
  2639. out_err:
  2640. for_each_buffer_cpu(buffer, cpu) {
  2641. struct buffer_page *bpage, *tmp;
  2642. cpu_buffer = buffer->buffers[cpu];
  2643. cpu_buffer->nr_pages_to_update = 0;
  2644. if (list_empty(&cpu_buffer->new_pages))
  2645. continue;
  2646. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  2647. list) {
  2648. list_del_init(&bpage->list);
  2649. free_buffer_page(bpage);
  2650. cond_resched();
  2651. }
  2652. }
  2653. out_err_unlock:
  2654. atomic_dec(&buffer->resizing);
  2655. mutex_unlock(&buffer->mutex);
  2656. return err;
  2657. }
  2658. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  2659. void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
  2660. {
  2661. mutex_lock(&buffer->mutex);
  2662. if (val)
  2663. buffer->flags |= RB_FL_OVERWRITE;
  2664. else
  2665. buffer->flags &= ~RB_FL_OVERWRITE;
  2666. mutex_unlock(&buffer->mutex);
  2667. }
  2668. EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
  2669. static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  2670. {
  2671. return bpage->page->data + index;
  2672. }
  2673. static __always_inline struct ring_buffer_event *
  2674. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  2675. {
  2676. return __rb_page_index(cpu_buffer->reader_page,
  2677. cpu_buffer->reader_page->read);
  2678. }
  2679. static struct ring_buffer_event *
  2680. rb_iter_head_event(struct ring_buffer_iter *iter)
  2681. {
  2682. struct ring_buffer_event *event;
  2683. struct buffer_page *iter_head_page = iter->head_page;
  2684. unsigned long commit;
  2685. unsigned length;
  2686. if (iter->head != iter->next_event)
  2687. return iter->event;
  2688. /*
  2689. * When the writer goes across pages, it issues a cmpxchg which
  2690. * is a mb(), which will synchronize with the rmb here.
  2691. * (see rb_tail_page_update() and __rb_reserve_next())
  2692. */
  2693. commit = rb_page_commit(iter_head_page);
  2694. smp_rmb();
  2695. /* An event needs to be at least 8 bytes in size */
  2696. if (iter->head > commit - 8)
  2697. goto reset;
  2698. event = __rb_page_index(iter_head_page, iter->head);
  2699. length = rb_event_length(event);
  2700. /*
  2701. * READ_ONCE() doesn't work on functions and we don't want the
  2702. * compiler doing any crazy optimizations with length.
  2703. */
  2704. barrier();
  2705. if ((iter->head + length) > commit || length > iter->event_size)
  2706. /* Writer corrupted the read? */
  2707. goto reset;
  2708. memcpy(iter->event, event, length);
  2709. /*
  2710. * If the page stamp is still the same after this rmb() then the
  2711. * event was safely copied without the writer entering the page.
  2712. */
  2713. smp_rmb();
  2714. /* Make sure the page didn't change since we read this */
  2715. if (iter->page_stamp != iter_head_page->page->time_stamp ||
  2716. commit > rb_page_commit(iter_head_page))
  2717. goto reset;
  2718. iter->next_event = iter->head + length;
  2719. return iter->event;
  2720. reset:
  2721. /* Reset to the beginning */
  2722. iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
  2723. iter->head = 0;
  2724. iter->next_event = 0;
  2725. iter->missed_events = 1;
  2726. return NULL;
  2727. }
  2728. /* Size is determined by what has been committed */
  2729. static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
  2730. {
  2731. return rb_page_commit(bpage) & ~RB_MISSED_MASK;
  2732. }
  2733. static __always_inline unsigned
  2734. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  2735. {
  2736. return rb_page_commit(cpu_buffer->commit_page);
  2737. }
  2738. static __always_inline unsigned
  2739. rb_event_index(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event *event)
  2740. {
  2741. unsigned long addr = (unsigned long)event;
  2742. addr &= (PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1;
  2743. return addr - BUF_PAGE_HDR_SIZE;
  2744. }
  2745. static void rb_inc_iter(struct ring_buffer_iter *iter)
  2746. {
  2747. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2748. /*
  2749. * The iterator could be on the reader page (it starts there).
  2750. * But the head could have moved, since the reader was
  2751. * found. Check for this case and assign the iterator
  2752. * to the head page instead of next.
  2753. */
  2754. if (iter->head_page == cpu_buffer->reader_page)
  2755. iter->head_page = rb_set_head_page(cpu_buffer);
  2756. else
  2757. rb_inc_page(&iter->head_page);
  2758. iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
  2759. iter->head = 0;
  2760. iter->next_event = 0;
  2761. }
  2762. /* Return the index into the sub-buffers for a given sub-buffer */
  2763. static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf)
  2764. {
  2765. void *subbuf_array;
  2766. subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs;
  2767. subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size);
  2768. return (subbuf - subbuf_array) / meta->subbuf_size;
  2769. }
  2770. static void rb_update_meta_head(struct ring_buffer_per_cpu *cpu_buffer,
  2771. struct buffer_page *next_page)
  2772. {
  2773. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  2774. unsigned long old_head = (unsigned long)next_page->page;
  2775. unsigned long new_head;
  2776. rb_inc_page(&next_page);
  2777. new_head = (unsigned long)next_page->page;
  2778. /*
  2779. * Only move it forward once, if something else came in and
  2780. * moved it forward, then we don't want to touch it.
  2781. */
  2782. (void)cmpxchg(&meta->head_buffer, old_head, new_head);
  2783. }
  2784. static void rb_update_meta_reader(struct ring_buffer_per_cpu *cpu_buffer,
  2785. struct buffer_page *reader)
  2786. {
  2787. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  2788. void *old_reader = cpu_buffer->reader_page->page;
  2789. void *new_reader = reader->page;
  2790. int id;
  2791. id = reader->id;
  2792. cpu_buffer->reader_page->id = id;
  2793. reader->id = 0;
  2794. meta->buffers[0] = rb_meta_subbuf_idx(meta, new_reader);
  2795. meta->buffers[id] = rb_meta_subbuf_idx(meta, old_reader);
  2796. /* The head pointer is the one after the reader */
  2797. rb_update_meta_head(cpu_buffer, reader);
  2798. }
  2799. /*
  2800. * rb_handle_head_page - writer hit the head page
  2801. *
  2802. * Returns: +1 to retry page
  2803. * 0 to continue
  2804. * -1 on error
  2805. */
  2806. static int
  2807. rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  2808. struct buffer_page *tail_page,
  2809. struct buffer_page *next_page)
  2810. {
  2811. struct buffer_page *new_head;
  2812. int entries;
  2813. int type;
  2814. int ret;
  2815. entries = rb_page_entries(next_page);
  2816. /*
  2817. * The hard part is here. We need to move the head
  2818. * forward, and protect against both readers on
  2819. * other CPUs and writers coming in via interrupts.
  2820. */
  2821. type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
  2822. RB_PAGE_HEAD);
  2823. /*
  2824. * type can be one of four:
  2825. * NORMAL - an interrupt already moved it for us
  2826. * HEAD - we are the first to get here.
  2827. * UPDATE - we are the interrupt interrupting
  2828. * a current move.
  2829. * MOVED - a reader on another CPU moved the next
  2830. * pointer to its reader page. Give up
  2831. * and try again.
  2832. */
  2833. switch (type) {
  2834. case RB_PAGE_HEAD:
  2835. /*
  2836. * We changed the head to UPDATE, thus
  2837. * it is our responsibility to update
  2838. * the counters.
  2839. */
  2840. local_add(entries, &cpu_buffer->overrun);
  2841. local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes);
  2842. local_inc(&cpu_buffer->pages_lost);
  2843. if (cpu_buffer->ring_meta)
  2844. rb_update_meta_head(cpu_buffer, next_page);
  2845. /*
  2846. * The entries will be zeroed out when we move the
  2847. * tail page.
  2848. */
  2849. /* still more to do */
  2850. break;
  2851. case RB_PAGE_UPDATE:
  2852. /*
  2853. * This is an interrupt that interrupt the
  2854. * previous update. Still more to do.
  2855. */
  2856. break;
  2857. case RB_PAGE_NORMAL:
  2858. /*
  2859. * An interrupt came in before the update
  2860. * and processed this for us.
  2861. * Nothing left to do.
  2862. */
  2863. return 1;
  2864. case RB_PAGE_MOVED:
  2865. /*
  2866. * The reader is on another CPU and just did
  2867. * a swap with our next_page.
  2868. * Try again.
  2869. */
  2870. return 1;
  2871. default:
  2872. RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
  2873. return -1;
  2874. }
  2875. /*
  2876. * Now that we are here, the old head pointer is
  2877. * set to UPDATE. This will keep the reader from
  2878. * swapping the head page with the reader page.
  2879. * The reader (on another CPU) will spin till
  2880. * we are finished.
  2881. *
  2882. * We just need to protect against interrupts
  2883. * doing the job. We will set the next pointer
  2884. * to HEAD. After that, we set the old pointer
  2885. * to NORMAL, but only if it was HEAD before.
  2886. * otherwise we are an interrupt, and only
  2887. * want the outer most commit to reset it.
  2888. */
  2889. new_head = next_page;
  2890. rb_inc_page(&new_head);
  2891. ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
  2892. RB_PAGE_NORMAL);
  2893. /*
  2894. * Valid returns are:
  2895. * HEAD - an interrupt came in and already set it.
  2896. * NORMAL - One of two things:
  2897. * 1) We really set it.
  2898. * 2) A bunch of interrupts came in and moved
  2899. * the page forward again.
  2900. */
  2901. switch (ret) {
  2902. case RB_PAGE_HEAD:
  2903. case RB_PAGE_NORMAL:
  2904. /* OK */
  2905. break;
  2906. default:
  2907. RB_WARN_ON(cpu_buffer, 1);
  2908. return -1;
  2909. }
  2910. /*
  2911. * It is possible that an interrupt came in,
  2912. * set the head up, then more interrupts came in
  2913. * and moved it again. When we get back here,
  2914. * the page would have been set to NORMAL but we
  2915. * just set it back to HEAD.
  2916. *
  2917. * How do you detect this? Well, if that happened
  2918. * the tail page would have moved.
  2919. */
  2920. if (ret == RB_PAGE_NORMAL) {
  2921. struct buffer_page *buffer_tail_page;
  2922. buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
  2923. /*
  2924. * If the tail had moved passed next, then we need
  2925. * to reset the pointer.
  2926. */
  2927. if (buffer_tail_page != tail_page &&
  2928. buffer_tail_page != next_page)
  2929. rb_head_page_set_normal(cpu_buffer, new_head,
  2930. next_page,
  2931. RB_PAGE_HEAD);
  2932. }
  2933. /*
  2934. * If this was the outer most commit (the one that
  2935. * changed the original pointer from HEAD to UPDATE),
  2936. * then it is up to us to reset it to NORMAL.
  2937. */
  2938. if (type == RB_PAGE_HEAD) {
  2939. ret = rb_head_page_set_normal(cpu_buffer, next_page,
  2940. tail_page,
  2941. RB_PAGE_UPDATE);
  2942. if (RB_WARN_ON(cpu_buffer,
  2943. ret != RB_PAGE_UPDATE))
  2944. return -1;
  2945. }
  2946. return 0;
  2947. }
  2948. static inline void
  2949. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  2950. unsigned long tail, struct rb_event_info *info)
  2951. {
  2952. unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
  2953. struct buffer_page *tail_page = info->tail_page;
  2954. struct ring_buffer_event *event;
  2955. unsigned long length = info->length;
  2956. /*
  2957. * Only the event that crossed the page boundary
  2958. * must fill the old tail_page with padding.
  2959. */
  2960. if (tail >= bsize) {
  2961. /*
  2962. * If the page was filled, then we still need
  2963. * to update the real_end. Reset it to zero
  2964. * and the reader will ignore it.
  2965. */
  2966. if (tail == bsize)
  2967. tail_page->real_end = 0;
  2968. local_sub(length, &tail_page->write);
  2969. return;
  2970. }
  2971. event = __rb_page_index(tail_page, tail);
  2972. /*
  2973. * Save the original length to the meta data.
  2974. * This will be used by the reader to add lost event
  2975. * counter.
  2976. */
  2977. tail_page->real_end = tail;
  2978. /*
  2979. * If this event is bigger than the minimum size, then
  2980. * we need to be careful that we don't subtract the
  2981. * write counter enough to allow another writer to slip
  2982. * in on this page.
  2983. * We put in a discarded commit instead, to make sure
  2984. * that this space is not used again, and this space will
  2985. * not be accounted into 'entries_bytes'.
  2986. *
  2987. * If we are less than the minimum size, we don't need to
  2988. * worry about it.
  2989. */
  2990. if (tail > (bsize - RB_EVNT_MIN_SIZE)) {
  2991. /* No room for any events */
  2992. /* Mark the rest of the page with padding */
  2993. rb_event_set_padding(event);
  2994. /* Make sure the padding is visible before the write update */
  2995. smp_wmb();
  2996. /* Set the write back to the previous setting */
  2997. local_sub(length, &tail_page->write);
  2998. return;
  2999. }
  3000. /* Put in a discarded event */
  3001. event->array[0] = (bsize - tail) - RB_EVNT_HDR_SIZE;
  3002. event->type_len = RINGBUF_TYPE_PADDING;
  3003. /* time delta must be non zero */
  3004. event->time_delta = 1;
  3005. /* account for padding bytes */
  3006. local_add(bsize - tail, &cpu_buffer->entries_bytes);
  3007. /* Make sure the padding is visible before the tail_page->write update */
  3008. smp_wmb();
  3009. /* Set write to end of buffer */
  3010. length = (tail + length) - bsize;
  3011. local_sub(length, &tail_page->write);
  3012. }
  3013. static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
  3014. /*
  3015. * This is the slow path, force gcc not to inline it.
  3016. */
  3017. static noinline struct ring_buffer_event *
  3018. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  3019. unsigned long tail, struct rb_event_info *info)
  3020. {
  3021. struct buffer_page *tail_page = info->tail_page;
  3022. struct buffer_page *commit_page = cpu_buffer->commit_page;
  3023. struct trace_buffer *buffer = cpu_buffer->buffer;
  3024. struct buffer_page *next_page;
  3025. int ret;
  3026. next_page = tail_page;
  3027. rb_inc_page(&next_page);
  3028. /*
  3029. * If for some reason, we had an interrupt storm that made
  3030. * it all the way around the buffer, bail, and warn
  3031. * about it.
  3032. */
  3033. if (unlikely(next_page == commit_page)) {
  3034. local_inc(&cpu_buffer->commit_overrun);
  3035. goto out_reset;
  3036. }
  3037. /*
  3038. * This is where the fun begins!
  3039. *
  3040. * We are fighting against races between a reader that
  3041. * could be on another CPU trying to swap its reader
  3042. * page with the buffer head.
  3043. *
  3044. * We are also fighting against interrupts coming in and
  3045. * moving the head or tail on us as well.
  3046. *
  3047. * If the next page is the head page then we have filled
  3048. * the buffer, unless the commit page is still on the
  3049. * reader page.
  3050. */
  3051. if (rb_is_head_page(next_page, &tail_page->list)) {
  3052. /*
  3053. * If the commit is not on the reader page, then
  3054. * move the header page.
  3055. */
  3056. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  3057. /*
  3058. * If we are not in overwrite mode,
  3059. * this is easy, just stop here.
  3060. */
  3061. if (!(buffer->flags & RB_FL_OVERWRITE)) {
  3062. local_inc(&cpu_buffer->dropped_events);
  3063. goto out_reset;
  3064. }
  3065. ret = rb_handle_head_page(cpu_buffer,
  3066. tail_page,
  3067. next_page);
  3068. if (ret < 0)
  3069. goto out_reset;
  3070. if (ret)
  3071. goto out_again;
  3072. } else {
  3073. /*
  3074. * We need to be careful here too. The
  3075. * commit page could still be on the reader
  3076. * page. We could have a small buffer, and
  3077. * have filled up the buffer with events
  3078. * from interrupts and such, and wrapped.
  3079. *
  3080. * Note, if the tail page is also on the
  3081. * reader_page, we let it move out.
  3082. */
  3083. if (unlikely((cpu_buffer->commit_page !=
  3084. cpu_buffer->tail_page) &&
  3085. (cpu_buffer->commit_page ==
  3086. cpu_buffer->reader_page))) {
  3087. local_inc(&cpu_buffer->commit_overrun);
  3088. goto out_reset;
  3089. }
  3090. }
  3091. }
  3092. rb_tail_page_update(cpu_buffer, tail_page, next_page);
  3093. out_again:
  3094. rb_reset_tail(cpu_buffer, tail, info);
  3095. /* Commit what we have for now. */
  3096. rb_end_commit(cpu_buffer);
  3097. /* rb_end_commit() decs committing */
  3098. local_inc(&cpu_buffer->committing);
  3099. /* fail and let the caller try again */
  3100. return ERR_PTR(-EAGAIN);
  3101. out_reset:
  3102. /* reset write */
  3103. rb_reset_tail(cpu_buffer, tail, info);
  3104. return NULL;
  3105. }
  3106. /* Slow path */
  3107. static struct ring_buffer_event *
  3108. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  3109. struct ring_buffer_event *event, u64 delta, bool abs)
  3110. {
  3111. if (abs)
  3112. event->type_len = RINGBUF_TYPE_TIME_STAMP;
  3113. else
  3114. event->type_len = RINGBUF_TYPE_TIME_EXTEND;
  3115. /* Not the first event on the page, or not delta? */
  3116. if (abs || rb_event_index(cpu_buffer, event)) {
  3117. event->time_delta = delta & TS_MASK;
  3118. event->array[0] = delta >> TS_SHIFT;
  3119. } else {
  3120. /* nope, just zero it */
  3121. event->time_delta = 0;
  3122. event->array[0] = 0;
  3123. }
  3124. return skip_time_extend(event);
  3125. }
  3126. #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  3127. static inline bool sched_clock_stable(void)
  3128. {
  3129. return true;
  3130. }
  3131. #endif
  3132. static void
  3133. rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
  3134. struct rb_event_info *info)
  3135. {
  3136. u64 write_stamp;
  3137. WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
  3138. (unsigned long long)info->delta,
  3139. (unsigned long long)info->ts,
  3140. (unsigned long long)info->before,
  3141. (unsigned long long)info->after,
  3142. (unsigned long long)({rb_time_read(&cpu_buffer->write_stamp, &write_stamp); write_stamp;}),
  3143. sched_clock_stable() ? "" :
  3144. "If you just came from a suspend/resume,\n"
  3145. "please switch to the trace global clock:\n"
  3146. " echo global > /sys/kernel/tracing/trace_clock\n"
  3147. "or add trace_clock=global to the kernel command line\n");
  3148. }
  3149. static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
  3150. struct ring_buffer_event **event,
  3151. struct rb_event_info *info,
  3152. u64 *delta,
  3153. unsigned int *length)
  3154. {
  3155. bool abs = info->add_timestamp &
  3156. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
  3157. if (unlikely(info->delta > (1ULL << 59))) {
  3158. /*
  3159. * Some timers can use more than 59 bits, and when a timestamp
  3160. * is added to the buffer, it will lose those bits.
  3161. */
  3162. if (abs && (info->ts & TS_MSB)) {
  3163. info->delta &= ABS_TS_MASK;
  3164. /* did the clock go backwards */
  3165. } else if (info->before == info->after && info->before > info->ts) {
  3166. /* not interrupted */
  3167. static int once;
  3168. /*
  3169. * This is possible with a recalibrating of the TSC.
  3170. * Do not produce a call stack, but just report it.
  3171. */
  3172. if (!once) {
  3173. once++;
  3174. pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
  3175. info->before, info->ts);
  3176. }
  3177. } else
  3178. rb_check_timestamp(cpu_buffer, info);
  3179. if (!abs)
  3180. info->delta = 0;
  3181. }
  3182. *event = rb_add_time_stamp(cpu_buffer, *event, info->delta, abs);
  3183. *length -= RB_LEN_TIME_EXTEND;
  3184. *delta = 0;
  3185. }
  3186. /**
  3187. * rb_update_event - update event type and data
  3188. * @cpu_buffer: The per cpu buffer of the @event
  3189. * @event: the event to update
  3190. * @info: The info to update the @event with (contains length and delta)
  3191. *
  3192. * Update the type and data fields of the @event. The length
  3193. * is the actual size that is written to the ring buffer,
  3194. * and with this, we can determine what to place into the
  3195. * data field.
  3196. */
  3197. static void
  3198. rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
  3199. struct ring_buffer_event *event,
  3200. struct rb_event_info *info)
  3201. {
  3202. unsigned length = info->length;
  3203. u64 delta = info->delta;
  3204. unsigned int nest = local_read(&cpu_buffer->committing) - 1;
  3205. if (!WARN_ON_ONCE(nest >= MAX_NEST))
  3206. cpu_buffer->event_stamp[nest] = info->ts;
  3207. /*
  3208. * If we need to add a timestamp, then we
  3209. * add it to the start of the reserved space.
  3210. */
  3211. if (unlikely(info->add_timestamp))
  3212. rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
  3213. event->time_delta = delta;
  3214. length -= RB_EVNT_HDR_SIZE;
  3215. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
  3216. event->type_len = 0;
  3217. event->array[0] = length;
  3218. } else
  3219. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  3220. }
  3221. static unsigned rb_calculate_event_length(unsigned length)
  3222. {
  3223. struct ring_buffer_event event; /* Used only for sizeof array */
  3224. /* zero length can cause confusions */
  3225. if (!length)
  3226. length++;
  3227. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
  3228. length += sizeof(event.array[0]);
  3229. length += RB_EVNT_HDR_SIZE;
  3230. length = ALIGN(length, RB_ARCH_ALIGNMENT);
  3231. /*
  3232. * In case the time delta is larger than the 27 bits for it
  3233. * in the header, we need to add a timestamp. If another
  3234. * event comes in when trying to discard this one to increase
  3235. * the length, then the timestamp will be added in the allocated
  3236. * space of this event. If length is bigger than the size needed
  3237. * for the TIME_EXTEND, then padding has to be used. The events
  3238. * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
  3239. * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
  3240. * As length is a multiple of 4, we only need to worry if it
  3241. * is 12 (RB_LEN_TIME_EXTEND + 4).
  3242. */
  3243. if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
  3244. length += RB_ALIGNMENT;
  3245. return length;
  3246. }
  3247. static inline bool
  3248. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  3249. struct ring_buffer_event *event)
  3250. {
  3251. unsigned long new_index, old_index;
  3252. struct buffer_page *bpage;
  3253. unsigned long addr;
  3254. new_index = rb_event_index(cpu_buffer, event);
  3255. old_index = new_index + rb_event_ts_length(event);
  3256. addr = (unsigned long)event;
  3257. addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
  3258. bpage = READ_ONCE(cpu_buffer->tail_page);
  3259. /*
  3260. * Make sure the tail_page is still the same and
  3261. * the next write location is the end of this event
  3262. */
  3263. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  3264. unsigned long write_mask =
  3265. local_read(&bpage->write) & ~RB_WRITE_MASK;
  3266. unsigned long event_length = rb_event_length(event);
  3267. /*
  3268. * For the before_stamp to be different than the write_stamp
  3269. * to make sure that the next event adds an absolute
  3270. * value and does not rely on the saved write stamp, which
  3271. * is now going to be bogus.
  3272. *
  3273. * By setting the before_stamp to zero, the next event
  3274. * is not going to use the write_stamp and will instead
  3275. * create an absolute timestamp. This means there's no
  3276. * reason to update the wirte_stamp!
  3277. */
  3278. rb_time_set(&cpu_buffer->before_stamp, 0);
  3279. /*
  3280. * If an event were to come in now, it would see that the
  3281. * write_stamp and the before_stamp are different, and assume
  3282. * that this event just added itself before updating
  3283. * the write stamp. The interrupting event will fix the
  3284. * write stamp for us, and use an absolute timestamp.
  3285. */
  3286. /*
  3287. * This is on the tail page. It is possible that
  3288. * a write could come in and move the tail page
  3289. * and write to the next page. That is fine
  3290. * because we just shorten what is on this page.
  3291. */
  3292. old_index += write_mask;
  3293. new_index += write_mask;
  3294. /* caution: old_index gets updated on cmpxchg failure */
  3295. if (local_try_cmpxchg(&bpage->write, &old_index, new_index)) {
  3296. /* update counters */
  3297. local_sub(event_length, &cpu_buffer->entries_bytes);
  3298. return true;
  3299. }
  3300. }
  3301. /* could not discard */
  3302. return false;
  3303. }
  3304. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3305. {
  3306. local_inc(&cpu_buffer->committing);
  3307. local_inc(&cpu_buffer->commits);
  3308. }
  3309. static __always_inline void
  3310. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  3311. {
  3312. unsigned long max_count;
  3313. /*
  3314. * We only race with interrupts and NMIs on this CPU.
  3315. * If we own the commit event, then we can commit
  3316. * all others that interrupted us, since the interruptions
  3317. * are in stack format (they finish before they come
  3318. * back to us). This allows us to do a simple loop to
  3319. * assign the commit to the tail.
  3320. */
  3321. again:
  3322. max_count = cpu_buffer->nr_pages * 100;
  3323. while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
  3324. if (RB_WARN_ON(cpu_buffer, !(--max_count)))
  3325. return;
  3326. if (RB_WARN_ON(cpu_buffer,
  3327. rb_is_reader_page(cpu_buffer->tail_page)))
  3328. return;
  3329. /*
  3330. * No need for a memory barrier here, as the update
  3331. * of the tail_page did it for this page.
  3332. */
  3333. local_set(&cpu_buffer->commit_page->page->commit,
  3334. rb_page_write(cpu_buffer->commit_page));
  3335. rb_inc_page(&cpu_buffer->commit_page);
  3336. if (cpu_buffer->ring_meta) {
  3337. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  3338. meta->commit_buffer = (unsigned long)cpu_buffer->commit_page->page;
  3339. }
  3340. /* add barrier to keep gcc from optimizing too much */
  3341. barrier();
  3342. }
  3343. while (rb_commit_index(cpu_buffer) !=
  3344. rb_page_write(cpu_buffer->commit_page)) {
  3345. /* Make sure the readers see the content of what is committed. */
  3346. smp_wmb();
  3347. local_set(&cpu_buffer->commit_page->page->commit,
  3348. rb_page_write(cpu_buffer->commit_page));
  3349. RB_WARN_ON(cpu_buffer,
  3350. local_read(&cpu_buffer->commit_page->page->commit) &
  3351. ~RB_WRITE_MASK);
  3352. barrier();
  3353. }
  3354. /* again, keep gcc from optimizing */
  3355. barrier();
  3356. /*
  3357. * If an interrupt came in just after the first while loop
  3358. * and pushed the tail page forward, we will be left with
  3359. * a dangling commit that will never go forward.
  3360. */
  3361. if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
  3362. goto again;
  3363. }
  3364. static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3365. {
  3366. unsigned long commits;
  3367. if (RB_WARN_ON(cpu_buffer,
  3368. !local_read(&cpu_buffer->committing)))
  3369. return;
  3370. again:
  3371. commits = local_read(&cpu_buffer->commits);
  3372. /* synchronize with interrupts */
  3373. barrier();
  3374. if (local_read(&cpu_buffer->committing) == 1)
  3375. rb_set_commit_to_write(cpu_buffer);
  3376. local_dec(&cpu_buffer->committing);
  3377. /* synchronize with interrupts */
  3378. barrier();
  3379. /*
  3380. * Need to account for interrupts coming in between the
  3381. * updating of the commit page and the clearing of the
  3382. * committing counter.
  3383. */
  3384. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  3385. !local_read(&cpu_buffer->committing)) {
  3386. local_inc(&cpu_buffer->committing);
  3387. goto again;
  3388. }
  3389. }
  3390. static inline void rb_event_discard(struct ring_buffer_event *event)
  3391. {
  3392. if (extended_time(event))
  3393. event = skip_time_extend(event);
  3394. /* array[0] holds the actual length for the discarded event */
  3395. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  3396. event->type_len = RINGBUF_TYPE_PADDING;
  3397. /* time delta must be non zero */
  3398. if (!event->time_delta)
  3399. event->time_delta = 1;
  3400. }
  3401. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3402. {
  3403. local_inc(&cpu_buffer->entries);
  3404. rb_end_commit(cpu_buffer);
  3405. }
  3406. static bool
  3407. rb_irq_work_queue(struct rb_irq_work *irq_work)
  3408. {
  3409. int cpu;
  3410. /* irq_work_queue_on() is not NMI-safe */
  3411. if (unlikely(in_nmi()))
  3412. return irq_work_queue(&irq_work->work);
  3413. /*
  3414. * If CPU isolation is not active, cpu is always the current
  3415. * CPU, and the following is equivallent to irq_work_queue().
  3416. */
  3417. cpu = housekeeping_any_cpu(HK_TYPE_KERNEL_NOISE);
  3418. return irq_work_queue_on(&irq_work->work, cpu);
  3419. }
  3420. static __always_inline void
  3421. rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
  3422. {
  3423. if (buffer->irq_work.waiters_pending) {
  3424. buffer->irq_work.waiters_pending = false;
  3425. /* irq_work_queue() supplies it's own memory barriers */
  3426. rb_irq_work_queue(&buffer->irq_work);
  3427. }
  3428. if (cpu_buffer->irq_work.waiters_pending) {
  3429. cpu_buffer->irq_work.waiters_pending = false;
  3430. /* irq_work_queue() supplies it's own memory barriers */
  3431. rb_irq_work_queue(&cpu_buffer->irq_work);
  3432. }
  3433. if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
  3434. return;
  3435. if (cpu_buffer->reader_page == cpu_buffer->commit_page)
  3436. return;
  3437. if (!cpu_buffer->irq_work.full_waiters_pending)
  3438. return;
  3439. cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
  3440. if (!full_hit(buffer, cpu_buffer->cpu, cpu_buffer->shortest_full))
  3441. return;
  3442. cpu_buffer->irq_work.wakeup_full = true;
  3443. cpu_buffer->irq_work.full_waiters_pending = false;
  3444. /* irq_work_queue() supplies it's own memory barriers */
  3445. rb_irq_work_queue(&cpu_buffer->irq_work);
  3446. }
  3447. #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
  3448. # define do_ring_buffer_record_recursion() \
  3449. do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
  3450. #else
  3451. # define do_ring_buffer_record_recursion() do { } while (0)
  3452. #endif
  3453. /*
  3454. * The lock and unlock are done within a preempt disable section.
  3455. * The current_context per_cpu variable can only be modified
  3456. * by the current task between lock and unlock. But it can
  3457. * be modified more than once via an interrupt. To pass this
  3458. * information from the lock to the unlock without having to
  3459. * access the 'in_interrupt()' functions again (which do show
  3460. * a bit of overhead in something as critical as function tracing,
  3461. * we use a bitmask trick.
  3462. *
  3463. * bit 1 = NMI context
  3464. * bit 2 = IRQ context
  3465. * bit 3 = SoftIRQ context
  3466. * bit 4 = normal context.
  3467. *
  3468. * This works because this is the order of contexts that can
  3469. * preempt other contexts. A SoftIRQ never preempts an IRQ
  3470. * context.
  3471. *
  3472. * When the context is determined, the corresponding bit is
  3473. * checked and set (if it was set, then a recursion of that context
  3474. * happened).
  3475. *
  3476. * On unlock, we need to clear this bit. To do so, just subtract
  3477. * 1 from the current_context and AND it to itself.
  3478. *
  3479. * (binary)
  3480. * 101 - 1 = 100
  3481. * 101 & 100 = 100 (clearing bit zero)
  3482. *
  3483. * 1010 - 1 = 1001
  3484. * 1010 & 1001 = 1000 (clearing bit 1)
  3485. *
  3486. * The least significant bit can be cleared this way, and it
  3487. * just so happens that it is the same bit corresponding to
  3488. * the current context.
  3489. *
  3490. * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
  3491. * is set when a recursion is detected at the current context, and if
  3492. * the TRANSITION bit is already set, it will fail the recursion.
  3493. * This is needed because there's a lag between the changing of
  3494. * interrupt context and updating the preempt count. In this case,
  3495. * a false positive will be found. To handle this, one extra recursion
  3496. * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
  3497. * bit is already set, then it is considered a recursion and the function
  3498. * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
  3499. *
  3500. * On the trace_recursive_unlock(), the TRANSITION bit will be the first
  3501. * to be cleared. Even if it wasn't the context that set it. That is,
  3502. * if an interrupt comes in while NORMAL bit is set and the ring buffer
  3503. * is called before preempt_count() is updated, since the check will
  3504. * be on the NORMAL bit, the TRANSITION bit will then be set. If an
  3505. * NMI then comes in, it will set the NMI bit, but when the NMI code
  3506. * does the trace_recursive_unlock() it will clear the TRANSITION bit
  3507. * and leave the NMI bit set. But this is fine, because the interrupt
  3508. * code that set the TRANSITION bit will then clear the NMI bit when it
  3509. * calls trace_recursive_unlock(). If another NMI comes in, it will
  3510. * set the TRANSITION bit and continue.
  3511. *
  3512. * Note: The TRANSITION bit only handles a single transition between context.
  3513. */
  3514. static __always_inline bool
  3515. trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
  3516. {
  3517. unsigned int val = cpu_buffer->current_context;
  3518. int bit = interrupt_context_level();
  3519. bit = RB_CTX_NORMAL - bit;
  3520. if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
  3521. /*
  3522. * It is possible that this was called by transitioning
  3523. * between interrupt context, and preempt_count() has not
  3524. * been updated yet. In this case, use the TRANSITION bit.
  3525. */
  3526. bit = RB_CTX_TRANSITION;
  3527. if (val & (1 << (bit + cpu_buffer->nest))) {
  3528. do_ring_buffer_record_recursion();
  3529. return true;
  3530. }
  3531. }
  3532. val |= (1 << (bit + cpu_buffer->nest));
  3533. cpu_buffer->current_context = val;
  3534. return false;
  3535. }
  3536. static __always_inline void
  3537. trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
  3538. {
  3539. cpu_buffer->current_context &=
  3540. cpu_buffer->current_context - (1 << cpu_buffer->nest);
  3541. }
  3542. /* The recursive locking above uses 5 bits */
  3543. #define NESTED_BITS 5
  3544. /**
  3545. * ring_buffer_nest_start - Allow to trace while nested
  3546. * @buffer: The ring buffer to modify
  3547. *
  3548. * The ring buffer has a safety mechanism to prevent recursion.
  3549. * But there may be a case where a trace needs to be done while
  3550. * tracing something else. In this case, calling this function
  3551. * will allow this function to nest within a currently active
  3552. * ring_buffer_lock_reserve().
  3553. *
  3554. * Call this function before calling another ring_buffer_lock_reserve() and
  3555. * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
  3556. */
  3557. void ring_buffer_nest_start(struct trace_buffer *buffer)
  3558. {
  3559. struct ring_buffer_per_cpu *cpu_buffer;
  3560. int cpu;
  3561. /* Enabled by ring_buffer_nest_end() */
  3562. preempt_disable_notrace();
  3563. cpu = raw_smp_processor_id();
  3564. cpu_buffer = buffer->buffers[cpu];
  3565. /* This is the shift value for the above recursive locking */
  3566. cpu_buffer->nest += NESTED_BITS;
  3567. }
  3568. /**
  3569. * ring_buffer_nest_end - Allow to trace while nested
  3570. * @buffer: The ring buffer to modify
  3571. *
  3572. * Must be called after ring_buffer_nest_start() and after the
  3573. * ring_buffer_unlock_commit().
  3574. */
  3575. void ring_buffer_nest_end(struct trace_buffer *buffer)
  3576. {
  3577. struct ring_buffer_per_cpu *cpu_buffer;
  3578. int cpu;
  3579. /* disabled by ring_buffer_nest_start() */
  3580. cpu = raw_smp_processor_id();
  3581. cpu_buffer = buffer->buffers[cpu];
  3582. /* This is the shift value for the above recursive locking */
  3583. cpu_buffer->nest -= NESTED_BITS;
  3584. preempt_enable_notrace();
  3585. }
  3586. /**
  3587. * ring_buffer_unlock_commit - commit a reserved
  3588. * @buffer: The buffer to commit to
  3589. *
  3590. * This commits the data to the ring buffer, and releases any locks held.
  3591. *
  3592. * Must be paired with ring_buffer_lock_reserve.
  3593. */
  3594. int ring_buffer_unlock_commit(struct trace_buffer *buffer)
  3595. {
  3596. struct ring_buffer_per_cpu *cpu_buffer;
  3597. int cpu = raw_smp_processor_id();
  3598. cpu_buffer = buffer->buffers[cpu];
  3599. rb_commit(cpu_buffer);
  3600. rb_wakeups(buffer, cpu_buffer);
  3601. trace_recursive_unlock(cpu_buffer);
  3602. preempt_enable_notrace();
  3603. return 0;
  3604. }
  3605. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  3606. /* Special value to validate all deltas on a page. */
  3607. #define CHECK_FULL_PAGE 1L
  3608. #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
  3609. static const char *show_irq_str(int bits)
  3610. {
  3611. static const char * type[] = {
  3612. ".", // 0
  3613. "s", // 1
  3614. "h", // 2
  3615. "Hs", // 3
  3616. "n", // 4
  3617. "Ns", // 5
  3618. "Nh", // 6
  3619. "NHs", // 7
  3620. };
  3621. return type[bits];
  3622. }
  3623. /* Assume this is a trace event */
  3624. static const char *show_flags(struct ring_buffer_event *event)
  3625. {
  3626. struct trace_entry *entry;
  3627. int bits = 0;
  3628. if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
  3629. return "X";
  3630. entry = ring_buffer_event_data(event);
  3631. if (entry->flags & TRACE_FLAG_SOFTIRQ)
  3632. bits |= 1;
  3633. if (entry->flags & TRACE_FLAG_HARDIRQ)
  3634. bits |= 2;
  3635. if (entry->flags & TRACE_FLAG_NMI)
  3636. bits |= 4;
  3637. return show_irq_str(bits);
  3638. }
  3639. static const char *show_irq(struct ring_buffer_event *event)
  3640. {
  3641. struct trace_entry *entry;
  3642. if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
  3643. return "";
  3644. entry = ring_buffer_event_data(event);
  3645. if (entry->flags & TRACE_FLAG_IRQS_OFF)
  3646. return "d";
  3647. return "";
  3648. }
  3649. static const char *show_interrupt_level(void)
  3650. {
  3651. unsigned long pc = preempt_count();
  3652. unsigned char level = 0;
  3653. if (pc & SOFTIRQ_OFFSET)
  3654. level |= 1;
  3655. if (pc & HARDIRQ_MASK)
  3656. level |= 2;
  3657. if (pc & NMI_MASK)
  3658. level |= 4;
  3659. return show_irq_str(level);
  3660. }
  3661. static void dump_buffer_page(struct buffer_data_page *bpage,
  3662. struct rb_event_info *info,
  3663. unsigned long tail)
  3664. {
  3665. struct ring_buffer_event *event;
  3666. u64 ts, delta;
  3667. int e;
  3668. ts = bpage->time_stamp;
  3669. pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
  3670. for (e = 0; e < tail; e += rb_event_length(event)) {
  3671. event = (struct ring_buffer_event *)(bpage->data + e);
  3672. switch (event->type_len) {
  3673. case RINGBUF_TYPE_TIME_EXTEND:
  3674. delta = rb_event_time_stamp(event);
  3675. ts += delta;
  3676. pr_warn(" 0x%x: [%lld] delta:%lld TIME EXTEND\n",
  3677. e, ts, delta);
  3678. break;
  3679. case RINGBUF_TYPE_TIME_STAMP:
  3680. delta = rb_event_time_stamp(event);
  3681. ts = rb_fix_abs_ts(delta, ts);
  3682. pr_warn(" 0x%x: [%lld] absolute:%lld TIME STAMP\n",
  3683. e, ts, delta);
  3684. break;
  3685. case RINGBUF_TYPE_PADDING:
  3686. ts += event->time_delta;
  3687. pr_warn(" 0x%x: [%lld] delta:%d PADDING\n",
  3688. e, ts, event->time_delta);
  3689. break;
  3690. case RINGBUF_TYPE_DATA:
  3691. ts += event->time_delta;
  3692. pr_warn(" 0x%x: [%lld] delta:%d %s%s\n",
  3693. e, ts, event->time_delta,
  3694. show_flags(event), show_irq(event));
  3695. break;
  3696. default:
  3697. break;
  3698. }
  3699. }
  3700. pr_warn("expected end:0x%lx last event actually ended at:0x%x\n", tail, e);
  3701. }
  3702. static DEFINE_PER_CPU(atomic_t, checking);
  3703. static atomic_t ts_dump;
  3704. #define buffer_warn_return(fmt, ...) \
  3705. do { \
  3706. /* If another report is happening, ignore this one */ \
  3707. if (atomic_inc_return(&ts_dump) != 1) { \
  3708. atomic_dec(&ts_dump); \
  3709. goto out; \
  3710. } \
  3711. atomic_inc(&cpu_buffer->record_disabled); \
  3712. pr_warn(fmt, ##__VA_ARGS__); \
  3713. dump_buffer_page(bpage, info, tail); \
  3714. atomic_dec(&ts_dump); \
  3715. /* There's some cases in boot up that this can happen */ \
  3716. if (WARN_ON_ONCE(system_state != SYSTEM_BOOTING)) \
  3717. /* Do not re-enable checking */ \
  3718. return; \
  3719. } while (0)
  3720. /*
  3721. * Check if the current event time stamp matches the deltas on
  3722. * the buffer page.
  3723. */
  3724. static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
  3725. struct rb_event_info *info,
  3726. unsigned long tail)
  3727. {
  3728. struct buffer_data_page *bpage;
  3729. u64 ts, delta;
  3730. bool full = false;
  3731. int ret;
  3732. bpage = info->tail_page->page;
  3733. if (tail == CHECK_FULL_PAGE) {
  3734. full = true;
  3735. tail = local_read(&bpage->commit);
  3736. } else if (info->add_timestamp &
  3737. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
  3738. /* Ignore events with absolute time stamps */
  3739. return;
  3740. }
  3741. /*
  3742. * Do not check the first event (skip possible extends too).
  3743. * Also do not check if previous events have not been committed.
  3744. */
  3745. if (tail <= 8 || tail > local_read(&bpage->commit))
  3746. return;
  3747. /*
  3748. * If this interrupted another event,
  3749. */
  3750. if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
  3751. goto out;
  3752. ret = rb_read_data_buffer(bpage, tail, cpu_buffer->cpu, &ts, &delta);
  3753. if (ret < 0) {
  3754. if (delta < ts) {
  3755. buffer_warn_return("[CPU: %d]ABSOLUTE TIME WENT BACKWARDS: last ts: %lld absolute ts: %lld\n",
  3756. cpu_buffer->cpu, ts, delta);
  3757. goto out;
  3758. }
  3759. }
  3760. if ((full && ts > info->ts) ||
  3761. (!full && ts + info->delta != info->ts)) {
  3762. buffer_warn_return("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s context:%s\n",
  3763. cpu_buffer->cpu,
  3764. ts + info->delta, info->ts, info->delta,
  3765. info->before, info->after,
  3766. full ? " (full)" : "", show_interrupt_level());
  3767. }
  3768. out:
  3769. atomic_dec(this_cpu_ptr(&checking));
  3770. }
  3771. #else
  3772. static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
  3773. struct rb_event_info *info,
  3774. unsigned long tail)
  3775. {
  3776. }
  3777. #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
  3778. static struct ring_buffer_event *
  3779. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  3780. struct rb_event_info *info)
  3781. {
  3782. struct ring_buffer_event *event;
  3783. struct buffer_page *tail_page;
  3784. unsigned long tail, write, w;
  3785. /* Don't let the compiler play games with cpu_buffer->tail_page */
  3786. tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
  3787. /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
  3788. barrier();
  3789. rb_time_read(&cpu_buffer->before_stamp, &info->before);
  3790. rb_time_read(&cpu_buffer->write_stamp, &info->after);
  3791. barrier();
  3792. info->ts = rb_time_stamp(cpu_buffer->buffer);
  3793. if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
  3794. info->delta = info->ts;
  3795. } else {
  3796. /*
  3797. * If interrupting an event time update, we may need an
  3798. * absolute timestamp.
  3799. * Don't bother if this is the start of a new page (w == 0).
  3800. */
  3801. if (!w) {
  3802. /* Use the sub-buffer timestamp */
  3803. info->delta = 0;
  3804. } else if (unlikely(info->before != info->after)) {
  3805. info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
  3806. info->length += RB_LEN_TIME_EXTEND;
  3807. } else {
  3808. info->delta = info->ts - info->after;
  3809. if (unlikely(test_time_stamp(info->delta))) {
  3810. info->add_timestamp |= RB_ADD_STAMP_EXTEND;
  3811. info->length += RB_LEN_TIME_EXTEND;
  3812. }
  3813. }
  3814. }
  3815. /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
  3816. /*C*/ write = local_add_return(info->length, &tail_page->write);
  3817. /* set write to only the index of the write */
  3818. write &= RB_WRITE_MASK;
  3819. tail = write - info->length;
  3820. /* See if we shot pass the end of this buffer page */
  3821. if (unlikely(write > cpu_buffer->buffer->subbuf_size)) {
  3822. check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
  3823. return rb_move_tail(cpu_buffer, tail, info);
  3824. }
  3825. if (likely(tail == w)) {
  3826. /* Nothing interrupted us between A and C */
  3827. /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
  3828. /*
  3829. * If something came in between C and D, the write stamp
  3830. * may now not be in sync. But that's fine as the before_stamp
  3831. * will be different and then next event will just be forced
  3832. * to use an absolute timestamp.
  3833. */
  3834. if (likely(!(info->add_timestamp &
  3835. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
  3836. /* This did not interrupt any time update */
  3837. info->delta = info->ts - info->after;
  3838. else
  3839. /* Just use full timestamp for interrupting event */
  3840. info->delta = info->ts;
  3841. check_buffer(cpu_buffer, info, tail);
  3842. } else {
  3843. u64 ts;
  3844. /* SLOW PATH - Interrupted between A and C */
  3845. /* Save the old before_stamp */
  3846. rb_time_read(&cpu_buffer->before_stamp, &info->before);
  3847. /*
  3848. * Read a new timestamp and update the before_stamp to make
  3849. * the next event after this one force using an absolute
  3850. * timestamp. This is in case an interrupt were to come in
  3851. * between E and F.
  3852. */
  3853. ts = rb_time_stamp(cpu_buffer->buffer);
  3854. rb_time_set(&cpu_buffer->before_stamp, ts);
  3855. barrier();
  3856. /*E*/ rb_time_read(&cpu_buffer->write_stamp, &info->after);
  3857. barrier();
  3858. /*F*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
  3859. info->after == info->before && info->after < ts) {
  3860. /*
  3861. * Nothing came after this event between C and F, it is
  3862. * safe to use info->after for the delta as it
  3863. * matched info->before and is still valid.
  3864. */
  3865. info->delta = ts - info->after;
  3866. } else {
  3867. /*
  3868. * Interrupted between C and F:
  3869. * Lost the previous events time stamp. Just set the
  3870. * delta to zero, and this will be the same time as
  3871. * the event this event interrupted. And the events that
  3872. * came after this will still be correct (as they would
  3873. * have built their delta on the previous event.
  3874. */
  3875. info->delta = 0;
  3876. }
  3877. info->ts = ts;
  3878. info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
  3879. }
  3880. /*
  3881. * If this is the first commit on the page, then it has the same
  3882. * timestamp as the page itself.
  3883. */
  3884. if (unlikely(!tail && !(info->add_timestamp &
  3885. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
  3886. info->delta = 0;
  3887. /* We reserved something on the buffer */
  3888. event = __rb_page_index(tail_page, tail);
  3889. rb_update_event(cpu_buffer, event, info);
  3890. local_inc(&tail_page->entries);
  3891. /*
  3892. * If this is the first commit on the page, then update
  3893. * its timestamp.
  3894. */
  3895. if (unlikely(!tail))
  3896. tail_page->page->time_stamp = info->ts;
  3897. /* account for these added bytes */
  3898. local_add(info->length, &cpu_buffer->entries_bytes);
  3899. return event;
  3900. }
  3901. static __always_inline struct ring_buffer_event *
  3902. rb_reserve_next_event(struct trace_buffer *buffer,
  3903. struct ring_buffer_per_cpu *cpu_buffer,
  3904. unsigned long length)
  3905. {
  3906. struct ring_buffer_event *event;
  3907. struct rb_event_info info;
  3908. int nr_loops = 0;
  3909. int add_ts_default;
  3910. /*
  3911. * ring buffer does cmpxchg as well as atomic64 operations
  3912. * (which some archs use locking for atomic64), make sure this
  3913. * is safe in NMI context
  3914. */
  3915. if ((!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) ||
  3916. IS_ENABLED(CONFIG_GENERIC_ATOMIC64)) &&
  3917. (unlikely(in_nmi()))) {
  3918. return NULL;
  3919. }
  3920. rb_start_commit(cpu_buffer);
  3921. /* The commit page can not change after this */
  3922. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3923. /*
  3924. * Due to the ability to swap a cpu buffer from a buffer
  3925. * it is possible it was swapped before we committed.
  3926. * (committing stops a swap). We check for it here and
  3927. * if it happened, we have to fail the write.
  3928. */
  3929. barrier();
  3930. if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
  3931. local_dec(&cpu_buffer->committing);
  3932. local_dec(&cpu_buffer->commits);
  3933. return NULL;
  3934. }
  3935. #endif
  3936. info.length = rb_calculate_event_length(length);
  3937. if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
  3938. add_ts_default = RB_ADD_STAMP_ABSOLUTE;
  3939. info.length += RB_LEN_TIME_EXTEND;
  3940. if (info.length > cpu_buffer->buffer->max_data_size)
  3941. goto out_fail;
  3942. } else {
  3943. add_ts_default = RB_ADD_STAMP_NONE;
  3944. }
  3945. again:
  3946. info.add_timestamp = add_ts_default;
  3947. info.delta = 0;
  3948. /*
  3949. * We allow for interrupts to reenter here and do a trace.
  3950. * If one does, it will cause this original code to loop
  3951. * back here. Even with heavy interrupts happening, this
  3952. * should only happen a few times in a row. If this happens
  3953. * 1000 times in a row, there must be either an interrupt
  3954. * storm or we have something buggy.
  3955. * Bail!
  3956. */
  3957. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  3958. goto out_fail;
  3959. event = __rb_reserve_next(cpu_buffer, &info);
  3960. if (unlikely(PTR_ERR(event) == -EAGAIN)) {
  3961. if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
  3962. info.length -= RB_LEN_TIME_EXTEND;
  3963. goto again;
  3964. }
  3965. if (likely(event))
  3966. return event;
  3967. out_fail:
  3968. rb_end_commit(cpu_buffer);
  3969. return NULL;
  3970. }
  3971. /**
  3972. * ring_buffer_lock_reserve - reserve a part of the buffer
  3973. * @buffer: the ring buffer to reserve from
  3974. * @length: the length of the data to reserve (excluding event header)
  3975. *
  3976. * Returns a reserved event on the ring buffer to copy directly to.
  3977. * The user of this interface will need to get the body to write into
  3978. * and can use the ring_buffer_event_data() interface.
  3979. *
  3980. * The length is the length of the data needed, not the event length
  3981. * which also includes the event header.
  3982. *
  3983. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  3984. * If NULL is returned, then nothing has been allocated or locked.
  3985. */
  3986. struct ring_buffer_event *
  3987. ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
  3988. {
  3989. struct ring_buffer_per_cpu *cpu_buffer;
  3990. struct ring_buffer_event *event;
  3991. int cpu;
  3992. /* If we are tracing schedule, we don't want to recurse */
  3993. preempt_disable_notrace();
  3994. if (unlikely(atomic_read(&buffer->record_disabled)))
  3995. goto out;
  3996. cpu = raw_smp_processor_id();
  3997. if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
  3998. goto out;
  3999. cpu_buffer = buffer->buffers[cpu];
  4000. if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
  4001. goto out;
  4002. if (unlikely(length > buffer->max_data_size))
  4003. goto out;
  4004. if (unlikely(trace_recursive_lock(cpu_buffer)))
  4005. goto out;
  4006. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  4007. if (!event)
  4008. goto out_unlock;
  4009. return event;
  4010. out_unlock:
  4011. trace_recursive_unlock(cpu_buffer);
  4012. out:
  4013. preempt_enable_notrace();
  4014. return NULL;
  4015. }
  4016. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  4017. /*
  4018. * Decrement the entries to the page that an event is on.
  4019. * The event does not even need to exist, only the pointer
  4020. * to the page it is on. This may only be called before the commit
  4021. * takes place.
  4022. */
  4023. static inline void
  4024. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  4025. struct ring_buffer_event *event)
  4026. {
  4027. unsigned long addr = (unsigned long)event;
  4028. struct buffer_page *bpage = cpu_buffer->commit_page;
  4029. struct buffer_page *start;
  4030. addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
  4031. /* Do the likely case first */
  4032. if (likely(bpage->page == (void *)addr)) {
  4033. local_dec(&bpage->entries);
  4034. return;
  4035. }
  4036. /*
  4037. * Because the commit page may be on the reader page we
  4038. * start with the next page and check the end loop there.
  4039. */
  4040. rb_inc_page(&bpage);
  4041. start = bpage;
  4042. do {
  4043. if (bpage->page == (void *)addr) {
  4044. local_dec(&bpage->entries);
  4045. return;
  4046. }
  4047. rb_inc_page(&bpage);
  4048. } while (bpage != start);
  4049. /* commit not part of this buffer?? */
  4050. RB_WARN_ON(cpu_buffer, 1);
  4051. }
  4052. /**
  4053. * ring_buffer_discard_commit - discard an event that has not been committed
  4054. * @buffer: the ring buffer
  4055. * @event: non committed event to discard
  4056. *
  4057. * Sometimes an event that is in the ring buffer needs to be ignored.
  4058. * This function lets the user discard an event in the ring buffer
  4059. * and then that event will not be read later.
  4060. *
  4061. * This function only works if it is called before the item has been
  4062. * committed. It will try to free the event from the ring buffer
  4063. * if another event has not been added behind it.
  4064. *
  4065. * If another event has been added behind it, it will set the event
  4066. * up as discarded, and perform the commit.
  4067. *
  4068. * If this function is called, do not call ring_buffer_unlock_commit on
  4069. * the event.
  4070. */
  4071. void ring_buffer_discard_commit(struct trace_buffer *buffer,
  4072. struct ring_buffer_event *event)
  4073. {
  4074. struct ring_buffer_per_cpu *cpu_buffer;
  4075. int cpu;
  4076. /* The event is discarded regardless */
  4077. rb_event_discard(event);
  4078. cpu = smp_processor_id();
  4079. cpu_buffer = buffer->buffers[cpu];
  4080. /*
  4081. * This must only be called if the event has not been
  4082. * committed yet. Thus we can assume that preemption
  4083. * is still disabled.
  4084. */
  4085. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  4086. rb_decrement_entry(cpu_buffer, event);
  4087. rb_try_to_discard(cpu_buffer, event);
  4088. rb_end_commit(cpu_buffer);
  4089. trace_recursive_unlock(cpu_buffer);
  4090. preempt_enable_notrace();
  4091. }
  4092. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  4093. /**
  4094. * ring_buffer_write - write data to the buffer without reserving
  4095. * @buffer: The ring buffer to write to.
  4096. * @length: The length of the data being written (excluding the event header)
  4097. * @data: The data to write to the buffer.
  4098. *
  4099. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  4100. * one function. If you already have the data to write to the buffer, it
  4101. * may be easier to simply call this function.
  4102. *
  4103. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  4104. * and not the length of the event which would hold the header.
  4105. */
  4106. int ring_buffer_write(struct trace_buffer *buffer,
  4107. unsigned long length,
  4108. void *data)
  4109. {
  4110. struct ring_buffer_per_cpu *cpu_buffer;
  4111. struct ring_buffer_event *event;
  4112. void *body;
  4113. int ret = -EBUSY;
  4114. int cpu;
  4115. guard(preempt_notrace)();
  4116. if (atomic_read(&buffer->record_disabled))
  4117. return -EBUSY;
  4118. cpu = raw_smp_processor_id();
  4119. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4120. return -EBUSY;
  4121. cpu_buffer = buffer->buffers[cpu];
  4122. if (atomic_read(&cpu_buffer->record_disabled))
  4123. return -EBUSY;
  4124. if (length > buffer->max_data_size)
  4125. return -EBUSY;
  4126. if (unlikely(trace_recursive_lock(cpu_buffer)))
  4127. return -EBUSY;
  4128. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  4129. if (!event)
  4130. goto out_unlock;
  4131. body = rb_event_data(event);
  4132. memcpy(body, data, length);
  4133. rb_commit(cpu_buffer);
  4134. rb_wakeups(buffer, cpu_buffer);
  4135. ret = 0;
  4136. out_unlock:
  4137. trace_recursive_unlock(cpu_buffer);
  4138. return ret;
  4139. }
  4140. EXPORT_SYMBOL_GPL(ring_buffer_write);
  4141. /*
  4142. * The total entries in the ring buffer is the running counter
  4143. * of entries entered into the ring buffer, minus the sum of
  4144. * the entries read from the ring buffer and the number of
  4145. * entries that were overwritten.
  4146. */
  4147. static inline unsigned long
  4148. rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
  4149. {
  4150. return local_read(&cpu_buffer->entries) -
  4151. (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
  4152. }
  4153. static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  4154. {
  4155. return !rb_num_of_entries(cpu_buffer);
  4156. }
  4157. /**
  4158. * ring_buffer_record_disable - stop all writes into the buffer
  4159. * @buffer: The ring buffer to stop writes to.
  4160. *
  4161. * This prevents all writes to the buffer. Any attempt to write
  4162. * to the buffer after this will fail and return NULL.
  4163. *
  4164. * The caller should call synchronize_rcu() after this.
  4165. */
  4166. void ring_buffer_record_disable(struct trace_buffer *buffer)
  4167. {
  4168. atomic_inc(&buffer->record_disabled);
  4169. }
  4170. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  4171. /**
  4172. * ring_buffer_record_enable - enable writes to the buffer
  4173. * @buffer: The ring buffer to enable writes
  4174. *
  4175. * Note, multiple disables will need the same number of enables
  4176. * to truly enable the writing (much like preempt_disable).
  4177. */
  4178. void ring_buffer_record_enable(struct trace_buffer *buffer)
  4179. {
  4180. atomic_dec(&buffer->record_disabled);
  4181. }
  4182. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  4183. /**
  4184. * ring_buffer_record_off - stop all writes into the buffer
  4185. * @buffer: The ring buffer to stop writes to.
  4186. *
  4187. * This prevents all writes to the buffer. Any attempt to write
  4188. * to the buffer after this will fail and return NULL.
  4189. *
  4190. * This is different than ring_buffer_record_disable() as
  4191. * it works like an on/off switch, where as the disable() version
  4192. * must be paired with a enable().
  4193. */
  4194. void ring_buffer_record_off(struct trace_buffer *buffer)
  4195. {
  4196. unsigned int rd;
  4197. unsigned int new_rd;
  4198. rd = atomic_read(&buffer->record_disabled);
  4199. do {
  4200. new_rd = rd | RB_BUFFER_OFF;
  4201. } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
  4202. }
  4203. EXPORT_SYMBOL_GPL(ring_buffer_record_off);
  4204. /**
  4205. * ring_buffer_record_on - restart writes into the buffer
  4206. * @buffer: The ring buffer to start writes to.
  4207. *
  4208. * This enables all writes to the buffer that was disabled by
  4209. * ring_buffer_record_off().
  4210. *
  4211. * This is different than ring_buffer_record_enable() as
  4212. * it works like an on/off switch, where as the enable() version
  4213. * must be paired with a disable().
  4214. */
  4215. void ring_buffer_record_on(struct trace_buffer *buffer)
  4216. {
  4217. unsigned int rd;
  4218. unsigned int new_rd;
  4219. rd = atomic_read(&buffer->record_disabled);
  4220. do {
  4221. new_rd = rd & ~RB_BUFFER_OFF;
  4222. } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
  4223. }
  4224. EXPORT_SYMBOL_GPL(ring_buffer_record_on);
  4225. /**
  4226. * ring_buffer_record_is_on - return true if the ring buffer can write
  4227. * @buffer: The ring buffer to see if write is enabled
  4228. *
  4229. * Returns true if the ring buffer is in a state that it accepts writes.
  4230. */
  4231. bool ring_buffer_record_is_on(struct trace_buffer *buffer)
  4232. {
  4233. return !atomic_read(&buffer->record_disabled);
  4234. }
  4235. /**
  4236. * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
  4237. * @buffer: The ring buffer to see if write is set enabled
  4238. *
  4239. * Returns true if the ring buffer is set writable by ring_buffer_record_on().
  4240. * Note that this does NOT mean it is in a writable state.
  4241. *
  4242. * It may return true when the ring buffer has been disabled by
  4243. * ring_buffer_record_disable(), as that is a temporary disabling of
  4244. * the ring buffer.
  4245. */
  4246. bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
  4247. {
  4248. return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
  4249. }
  4250. /**
  4251. * ring_buffer_record_is_on_cpu - return true if the ring buffer can write
  4252. * @buffer: The ring buffer to see if write is enabled
  4253. * @cpu: The CPU to test if the ring buffer can write too
  4254. *
  4255. * Returns true if the ring buffer is in a state that it accepts writes
  4256. * for a particular CPU.
  4257. */
  4258. bool ring_buffer_record_is_on_cpu(struct trace_buffer *buffer, int cpu)
  4259. {
  4260. struct ring_buffer_per_cpu *cpu_buffer;
  4261. cpu_buffer = buffer->buffers[cpu];
  4262. return ring_buffer_record_is_set_on(buffer) &&
  4263. !atomic_read(&cpu_buffer->record_disabled);
  4264. }
  4265. /**
  4266. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  4267. * @buffer: The ring buffer to stop writes to.
  4268. * @cpu: The CPU buffer to stop
  4269. *
  4270. * This prevents all writes to the buffer. Any attempt to write
  4271. * to the buffer after this will fail and return NULL.
  4272. *
  4273. * The caller should call synchronize_rcu() after this.
  4274. */
  4275. void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
  4276. {
  4277. struct ring_buffer_per_cpu *cpu_buffer;
  4278. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4279. return;
  4280. cpu_buffer = buffer->buffers[cpu];
  4281. atomic_inc(&cpu_buffer->record_disabled);
  4282. }
  4283. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  4284. /**
  4285. * ring_buffer_record_enable_cpu - enable writes to the buffer
  4286. * @buffer: The ring buffer to enable writes
  4287. * @cpu: The CPU to enable.
  4288. *
  4289. * Note, multiple disables will need the same number of enables
  4290. * to truly enable the writing (much like preempt_disable).
  4291. */
  4292. void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
  4293. {
  4294. struct ring_buffer_per_cpu *cpu_buffer;
  4295. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4296. return;
  4297. cpu_buffer = buffer->buffers[cpu];
  4298. atomic_dec(&cpu_buffer->record_disabled);
  4299. }
  4300. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  4301. /**
  4302. * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
  4303. * @buffer: The ring buffer
  4304. * @cpu: The per CPU buffer to read from.
  4305. */
  4306. u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
  4307. {
  4308. unsigned long flags;
  4309. struct ring_buffer_per_cpu *cpu_buffer;
  4310. struct buffer_page *bpage;
  4311. u64 ret = 0;
  4312. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4313. return 0;
  4314. cpu_buffer = buffer->buffers[cpu];
  4315. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4316. /*
  4317. * if the tail is on reader_page, oldest time stamp is on the reader
  4318. * page
  4319. */
  4320. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  4321. bpage = cpu_buffer->reader_page;
  4322. else
  4323. bpage = rb_set_head_page(cpu_buffer);
  4324. if (bpage)
  4325. ret = bpage->page->time_stamp;
  4326. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  4327. return ret;
  4328. }
  4329. EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
  4330. /**
  4331. * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer
  4332. * @buffer: The ring buffer
  4333. * @cpu: The per CPU buffer to read from.
  4334. */
  4335. unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
  4336. {
  4337. struct ring_buffer_per_cpu *cpu_buffer;
  4338. unsigned long ret;
  4339. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4340. return 0;
  4341. cpu_buffer = buffer->buffers[cpu];
  4342. ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
  4343. return ret;
  4344. }
  4345. EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
  4346. /**
  4347. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  4348. * @buffer: The ring buffer
  4349. * @cpu: The per CPU buffer to get the entries from.
  4350. */
  4351. unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
  4352. {
  4353. struct ring_buffer_per_cpu *cpu_buffer;
  4354. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4355. return 0;
  4356. cpu_buffer = buffer->buffers[cpu];
  4357. return rb_num_of_entries(cpu_buffer);
  4358. }
  4359. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  4360. /**
  4361. * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
  4362. * buffer wrapping around (only if RB_FL_OVERWRITE is on).
  4363. * @buffer: The ring buffer
  4364. * @cpu: The per CPU buffer to get the number of overruns from
  4365. */
  4366. unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
  4367. {
  4368. struct ring_buffer_per_cpu *cpu_buffer;
  4369. unsigned long ret;
  4370. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4371. return 0;
  4372. cpu_buffer = buffer->buffers[cpu];
  4373. ret = local_read(&cpu_buffer->overrun);
  4374. return ret;
  4375. }
  4376. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  4377. /**
  4378. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
  4379. * commits failing due to the buffer wrapping around while there are uncommitted
  4380. * events, such as during an interrupt storm.
  4381. * @buffer: The ring buffer
  4382. * @cpu: The per CPU buffer to get the number of overruns from
  4383. */
  4384. unsigned long
  4385. ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
  4386. {
  4387. struct ring_buffer_per_cpu *cpu_buffer;
  4388. unsigned long ret;
  4389. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4390. return 0;
  4391. cpu_buffer = buffer->buffers[cpu];
  4392. ret = local_read(&cpu_buffer->commit_overrun);
  4393. return ret;
  4394. }
  4395. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  4396. /**
  4397. * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
  4398. * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
  4399. * @buffer: The ring buffer
  4400. * @cpu: The per CPU buffer to get the number of overruns from
  4401. */
  4402. unsigned long
  4403. ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
  4404. {
  4405. struct ring_buffer_per_cpu *cpu_buffer;
  4406. unsigned long ret;
  4407. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4408. return 0;
  4409. cpu_buffer = buffer->buffers[cpu];
  4410. ret = local_read(&cpu_buffer->dropped_events);
  4411. return ret;
  4412. }
  4413. EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
  4414. /**
  4415. * ring_buffer_read_events_cpu - get the number of events successfully read
  4416. * @buffer: The ring buffer
  4417. * @cpu: The per CPU buffer to get the number of events read
  4418. */
  4419. unsigned long
  4420. ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
  4421. {
  4422. struct ring_buffer_per_cpu *cpu_buffer;
  4423. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4424. return 0;
  4425. cpu_buffer = buffer->buffers[cpu];
  4426. return cpu_buffer->read;
  4427. }
  4428. EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
  4429. /**
  4430. * ring_buffer_entries - get the number of entries in a buffer
  4431. * @buffer: The ring buffer
  4432. *
  4433. * Returns the total number of entries in the ring buffer
  4434. * (all CPU entries)
  4435. */
  4436. unsigned long ring_buffer_entries(struct trace_buffer *buffer)
  4437. {
  4438. struct ring_buffer_per_cpu *cpu_buffer;
  4439. unsigned long entries = 0;
  4440. int cpu;
  4441. /* if you care about this being correct, lock the buffer */
  4442. for_each_buffer_cpu(buffer, cpu) {
  4443. cpu_buffer = buffer->buffers[cpu];
  4444. entries += rb_num_of_entries(cpu_buffer);
  4445. }
  4446. return entries;
  4447. }
  4448. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  4449. /**
  4450. * ring_buffer_overruns - get the number of overruns in buffer
  4451. * @buffer: The ring buffer
  4452. *
  4453. * Returns the total number of overruns in the ring buffer
  4454. * (all CPU entries)
  4455. */
  4456. unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
  4457. {
  4458. struct ring_buffer_per_cpu *cpu_buffer;
  4459. unsigned long overruns = 0;
  4460. int cpu;
  4461. /* if you care about this being correct, lock the buffer */
  4462. for_each_buffer_cpu(buffer, cpu) {
  4463. cpu_buffer = buffer->buffers[cpu];
  4464. overruns += local_read(&cpu_buffer->overrun);
  4465. }
  4466. return overruns;
  4467. }
  4468. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  4469. static void rb_iter_reset(struct ring_buffer_iter *iter)
  4470. {
  4471. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  4472. /* Iterator usage is expected to have record disabled */
  4473. iter->head_page = cpu_buffer->reader_page;
  4474. iter->head = cpu_buffer->reader_page->read;
  4475. iter->next_event = iter->head;
  4476. iter->cache_reader_page = iter->head_page;
  4477. iter->cache_read = cpu_buffer->read;
  4478. iter->cache_pages_removed = cpu_buffer->pages_removed;
  4479. if (iter->head) {
  4480. iter->read_stamp = cpu_buffer->read_stamp;
  4481. iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
  4482. } else {
  4483. iter->read_stamp = iter->head_page->page->time_stamp;
  4484. iter->page_stamp = iter->read_stamp;
  4485. }
  4486. }
  4487. /**
  4488. * ring_buffer_iter_reset - reset an iterator
  4489. * @iter: The iterator to reset
  4490. *
  4491. * Resets the iterator, so that it will start from the beginning
  4492. * again.
  4493. */
  4494. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  4495. {
  4496. struct ring_buffer_per_cpu *cpu_buffer;
  4497. unsigned long flags;
  4498. if (!iter)
  4499. return;
  4500. cpu_buffer = iter->cpu_buffer;
  4501. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4502. rb_iter_reset(iter);
  4503. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  4504. }
  4505. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  4506. /**
  4507. * ring_buffer_iter_empty - check if an iterator has no more to read
  4508. * @iter: The iterator to check
  4509. */
  4510. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  4511. {
  4512. struct ring_buffer_per_cpu *cpu_buffer;
  4513. struct buffer_page *reader;
  4514. struct buffer_page *head_page;
  4515. struct buffer_page *commit_page;
  4516. struct buffer_page *curr_commit_page;
  4517. unsigned commit;
  4518. u64 curr_commit_ts;
  4519. u64 commit_ts;
  4520. cpu_buffer = iter->cpu_buffer;
  4521. reader = cpu_buffer->reader_page;
  4522. head_page = cpu_buffer->head_page;
  4523. commit_page = READ_ONCE(cpu_buffer->commit_page);
  4524. commit_ts = commit_page->page->time_stamp;
  4525. /*
  4526. * When the writer goes across pages, it issues a cmpxchg which
  4527. * is a mb(), which will synchronize with the rmb here.
  4528. * (see rb_tail_page_update())
  4529. */
  4530. smp_rmb();
  4531. commit = rb_page_commit(commit_page);
  4532. /* We want to make sure that the commit page doesn't change */
  4533. smp_rmb();
  4534. /* Make sure commit page didn't change */
  4535. curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
  4536. curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
  4537. /* If the commit page changed, then there's more data */
  4538. if (curr_commit_page != commit_page ||
  4539. curr_commit_ts != commit_ts)
  4540. return 0;
  4541. /* Still racy, as it may return a false positive, but that's OK */
  4542. return ((iter->head_page == commit_page && iter->head >= commit) ||
  4543. (iter->head_page == reader && commit_page == head_page &&
  4544. head_page->read == commit &&
  4545. iter->head == rb_page_size(cpu_buffer->reader_page)));
  4546. }
  4547. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  4548. static void
  4549. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  4550. struct ring_buffer_event *event)
  4551. {
  4552. u64 delta;
  4553. switch (event->type_len) {
  4554. case RINGBUF_TYPE_PADDING:
  4555. return;
  4556. case RINGBUF_TYPE_TIME_EXTEND:
  4557. delta = rb_event_time_stamp(event);
  4558. cpu_buffer->read_stamp += delta;
  4559. return;
  4560. case RINGBUF_TYPE_TIME_STAMP:
  4561. delta = rb_event_time_stamp(event);
  4562. delta = rb_fix_abs_ts(delta, cpu_buffer->read_stamp);
  4563. cpu_buffer->read_stamp = delta;
  4564. return;
  4565. case RINGBUF_TYPE_DATA:
  4566. cpu_buffer->read_stamp += event->time_delta;
  4567. return;
  4568. default:
  4569. RB_WARN_ON(cpu_buffer, 1);
  4570. }
  4571. }
  4572. static void
  4573. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  4574. struct ring_buffer_event *event)
  4575. {
  4576. u64 delta;
  4577. switch (event->type_len) {
  4578. case RINGBUF_TYPE_PADDING:
  4579. return;
  4580. case RINGBUF_TYPE_TIME_EXTEND:
  4581. delta = rb_event_time_stamp(event);
  4582. iter->read_stamp += delta;
  4583. return;
  4584. case RINGBUF_TYPE_TIME_STAMP:
  4585. delta = rb_event_time_stamp(event);
  4586. delta = rb_fix_abs_ts(delta, iter->read_stamp);
  4587. iter->read_stamp = delta;
  4588. return;
  4589. case RINGBUF_TYPE_DATA:
  4590. iter->read_stamp += event->time_delta;
  4591. return;
  4592. default:
  4593. RB_WARN_ON(iter->cpu_buffer, 1);
  4594. }
  4595. }
  4596. static struct buffer_page *
  4597. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  4598. {
  4599. struct buffer_page *reader = NULL;
  4600. unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
  4601. unsigned long overwrite;
  4602. unsigned long flags;
  4603. int nr_loops = 0;
  4604. bool ret;
  4605. local_irq_save(flags);
  4606. arch_spin_lock(&cpu_buffer->lock);
  4607. again:
  4608. /*
  4609. * This should normally only loop twice. But because the
  4610. * start of the reader inserts an empty page, it causes
  4611. * a case where we will loop three times. There should be no
  4612. * reason to loop four times (that I know of).
  4613. */
  4614. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  4615. reader = NULL;
  4616. goto out;
  4617. }
  4618. reader = cpu_buffer->reader_page;
  4619. /* If there's more to read, return this page */
  4620. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  4621. goto out;
  4622. /* Never should we have an index greater than the size */
  4623. if (RB_WARN_ON(cpu_buffer,
  4624. cpu_buffer->reader_page->read > rb_page_size(reader)))
  4625. goto out;
  4626. /* check if we caught up to the tail */
  4627. reader = NULL;
  4628. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  4629. goto out;
  4630. /* Don't bother swapping if the ring buffer is empty */
  4631. if (rb_num_of_entries(cpu_buffer) == 0)
  4632. goto out;
  4633. /*
  4634. * Reset the reader page to size zero.
  4635. */
  4636. local_set(&cpu_buffer->reader_page->write, 0);
  4637. local_set(&cpu_buffer->reader_page->entries, 0);
  4638. cpu_buffer->reader_page->real_end = 0;
  4639. spin:
  4640. /*
  4641. * Splice the empty reader page into the list around the head.
  4642. */
  4643. reader = rb_set_head_page(cpu_buffer);
  4644. if (!reader)
  4645. goto out;
  4646. cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
  4647. cpu_buffer->reader_page->list.prev = reader->list.prev;
  4648. /*
  4649. * cpu_buffer->pages just needs to point to the buffer, it
  4650. * has no specific buffer page to point to. Lets move it out
  4651. * of our way so we don't accidentally swap it.
  4652. */
  4653. cpu_buffer->pages = reader->list.prev;
  4654. /* The reader page will be pointing to the new head */
  4655. rb_set_list_to_head(&cpu_buffer->reader_page->list);
  4656. /*
  4657. * We want to make sure we read the overruns after we set up our
  4658. * pointers to the next object. The writer side does a
  4659. * cmpxchg to cross pages which acts as the mb on the writer
  4660. * side. Note, the reader will constantly fail the swap
  4661. * while the writer is updating the pointers, so this
  4662. * guarantees that the overwrite recorded here is the one we
  4663. * want to compare with the last_overrun.
  4664. */
  4665. smp_mb();
  4666. overwrite = local_read(&(cpu_buffer->overrun));
  4667. /*
  4668. * Here's the tricky part.
  4669. *
  4670. * We need to move the pointer past the header page.
  4671. * But we can only do that if a writer is not currently
  4672. * moving it. The page before the header page has the
  4673. * flag bit '1' set if it is pointing to the page we want.
  4674. * but if the writer is in the process of moving it
  4675. * then it will be '2' or already moved '0'.
  4676. */
  4677. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  4678. /*
  4679. * If we did not convert it, then we must try again.
  4680. */
  4681. if (!ret)
  4682. goto spin;
  4683. if (cpu_buffer->ring_meta)
  4684. rb_update_meta_reader(cpu_buffer, reader);
  4685. /*
  4686. * Yay! We succeeded in replacing the page.
  4687. *
  4688. * Now make the new head point back to the reader page.
  4689. */
  4690. rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
  4691. rb_inc_page(&cpu_buffer->head_page);
  4692. cpu_buffer->cnt++;
  4693. local_inc(&cpu_buffer->pages_read);
  4694. /* Finally update the reader page to the new head */
  4695. cpu_buffer->reader_page = reader;
  4696. cpu_buffer->reader_page->read = 0;
  4697. if (overwrite != cpu_buffer->last_overrun) {
  4698. cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
  4699. cpu_buffer->last_overrun = overwrite;
  4700. }
  4701. goto again;
  4702. out:
  4703. /* Update the read_stamp on the first event */
  4704. if (reader && reader->read == 0)
  4705. cpu_buffer->read_stamp = reader->page->time_stamp;
  4706. arch_spin_unlock(&cpu_buffer->lock);
  4707. local_irq_restore(flags);
  4708. /*
  4709. * The writer has preempt disable, wait for it. But not forever
  4710. * Although, 1 second is pretty much "forever"
  4711. */
  4712. #define USECS_WAIT 1000000
  4713. for (nr_loops = 0; nr_loops < USECS_WAIT; nr_loops++) {
  4714. /* If the write is past the end of page, a writer is still updating it */
  4715. if (likely(!reader || rb_page_write(reader) <= bsize))
  4716. break;
  4717. udelay(1);
  4718. /* Get the latest version of the reader write value */
  4719. smp_rmb();
  4720. }
  4721. /* The writer is not moving forward? Something is wrong */
  4722. if (RB_WARN_ON(cpu_buffer, nr_loops == USECS_WAIT))
  4723. reader = NULL;
  4724. /*
  4725. * Make sure we see any padding after the write update
  4726. * (see rb_reset_tail()).
  4727. *
  4728. * In addition, a writer may be writing on the reader page
  4729. * if the page has not been fully filled, so the read barrier
  4730. * is also needed to make sure we see the content of what is
  4731. * committed by the writer (see rb_set_commit_to_write()).
  4732. */
  4733. smp_rmb();
  4734. return reader;
  4735. }
  4736. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  4737. {
  4738. struct ring_buffer_event *event;
  4739. struct buffer_page *reader;
  4740. unsigned length;
  4741. reader = rb_get_reader_page(cpu_buffer);
  4742. /* This function should not be called when buffer is empty */
  4743. if (RB_WARN_ON(cpu_buffer, !reader))
  4744. return;
  4745. event = rb_reader_event(cpu_buffer);
  4746. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  4747. cpu_buffer->read++;
  4748. rb_update_read_stamp(cpu_buffer, event);
  4749. length = rb_event_length(event);
  4750. cpu_buffer->reader_page->read += length;
  4751. cpu_buffer->read_bytes += length;
  4752. }
  4753. static void rb_advance_iter(struct ring_buffer_iter *iter)
  4754. {
  4755. struct ring_buffer_per_cpu *cpu_buffer;
  4756. cpu_buffer = iter->cpu_buffer;
  4757. /* If head == next_event then we need to jump to the next event */
  4758. if (iter->head == iter->next_event) {
  4759. /* If the event gets overwritten again, there's nothing to do */
  4760. if (rb_iter_head_event(iter) == NULL)
  4761. return;
  4762. }
  4763. iter->head = iter->next_event;
  4764. /*
  4765. * Check if we are at the end of the buffer.
  4766. */
  4767. if (iter->next_event >= rb_page_size(iter->head_page)) {
  4768. /* discarded commits can make the page empty */
  4769. if (iter->head_page == cpu_buffer->commit_page)
  4770. return;
  4771. rb_inc_iter(iter);
  4772. return;
  4773. }
  4774. rb_update_iter_read_stamp(iter, iter->event);
  4775. }
  4776. static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
  4777. {
  4778. return cpu_buffer->lost_events;
  4779. }
  4780. static struct ring_buffer_event *
  4781. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
  4782. unsigned long *lost_events)
  4783. {
  4784. struct ring_buffer_event *event;
  4785. struct buffer_page *reader;
  4786. int nr_loops = 0;
  4787. if (ts)
  4788. *ts = 0;
  4789. again:
  4790. /*
  4791. * We repeat when a time extend is encountered.
  4792. * Since the time extend is always attached to a data event,
  4793. * we should never loop more than once.
  4794. * (We never hit the following condition more than twice).
  4795. */
  4796. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
  4797. return NULL;
  4798. reader = rb_get_reader_page(cpu_buffer);
  4799. if (!reader)
  4800. return NULL;
  4801. event = rb_reader_event(cpu_buffer);
  4802. switch (event->type_len) {
  4803. case RINGBUF_TYPE_PADDING:
  4804. if (rb_null_event(event))
  4805. RB_WARN_ON(cpu_buffer, 1);
  4806. /*
  4807. * Because the writer could be discarding every
  4808. * event it creates (which would probably be bad)
  4809. * if we were to go back to "again" then we may never
  4810. * catch up, and will trigger the warn on, or lock
  4811. * the box. Return the padding, and we will release
  4812. * the current locks, and try again.
  4813. */
  4814. return event;
  4815. case RINGBUF_TYPE_TIME_EXTEND:
  4816. /* Internal data, OK to advance */
  4817. rb_advance_reader(cpu_buffer);
  4818. goto again;
  4819. case RINGBUF_TYPE_TIME_STAMP:
  4820. if (ts) {
  4821. *ts = rb_event_time_stamp(event);
  4822. *ts = rb_fix_abs_ts(*ts, reader->page->time_stamp);
  4823. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4824. cpu_buffer->cpu, ts);
  4825. }
  4826. /* Internal data, OK to advance */
  4827. rb_advance_reader(cpu_buffer);
  4828. goto again;
  4829. case RINGBUF_TYPE_DATA:
  4830. if (ts && !(*ts)) {
  4831. *ts = cpu_buffer->read_stamp + event->time_delta;
  4832. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4833. cpu_buffer->cpu, ts);
  4834. }
  4835. if (lost_events)
  4836. *lost_events = rb_lost_events(cpu_buffer);
  4837. return event;
  4838. default:
  4839. RB_WARN_ON(cpu_buffer, 1);
  4840. }
  4841. return NULL;
  4842. }
  4843. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  4844. static struct ring_buffer_event *
  4845. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  4846. {
  4847. struct trace_buffer *buffer;
  4848. struct ring_buffer_per_cpu *cpu_buffer;
  4849. struct ring_buffer_event *event;
  4850. int nr_loops = 0;
  4851. if (ts)
  4852. *ts = 0;
  4853. cpu_buffer = iter->cpu_buffer;
  4854. buffer = cpu_buffer->buffer;
  4855. /*
  4856. * Check if someone performed a consuming read to the buffer
  4857. * or removed some pages from the buffer. In these cases,
  4858. * iterator was invalidated and we need to reset it.
  4859. */
  4860. if (unlikely(iter->cache_read != cpu_buffer->read ||
  4861. iter->cache_reader_page != cpu_buffer->reader_page ||
  4862. iter->cache_pages_removed != cpu_buffer->pages_removed))
  4863. rb_iter_reset(iter);
  4864. again:
  4865. if (ring_buffer_iter_empty(iter))
  4866. return NULL;
  4867. /*
  4868. * As the writer can mess with what the iterator is trying
  4869. * to read, just give up if we fail to get an event after
  4870. * three tries. The iterator is not as reliable when reading
  4871. * the ring buffer with an active write as the consumer is.
  4872. * Do not warn if the three failures is reached.
  4873. */
  4874. if (++nr_loops > 3)
  4875. return NULL;
  4876. if (rb_per_cpu_empty(cpu_buffer))
  4877. return NULL;
  4878. if (iter->head >= rb_page_size(iter->head_page)) {
  4879. rb_inc_iter(iter);
  4880. goto again;
  4881. }
  4882. event = rb_iter_head_event(iter);
  4883. if (!event)
  4884. goto again;
  4885. switch (event->type_len) {
  4886. case RINGBUF_TYPE_PADDING:
  4887. if (rb_null_event(event)) {
  4888. rb_inc_iter(iter);
  4889. goto again;
  4890. }
  4891. rb_advance_iter(iter);
  4892. return event;
  4893. case RINGBUF_TYPE_TIME_EXTEND:
  4894. /* Internal data, OK to advance */
  4895. rb_advance_iter(iter);
  4896. goto again;
  4897. case RINGBUF_TYPE_TIME_STAMP:
  4898. if (ts) {
  4899. *ts = rb_event_time_stamp(event);
  4900. *ts = rb_fix_abs_ts(*ts, iter->head_page->page->time_stamp);
  4901. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4902. cpu_buffer->cpu, ts);
  4903. }
  4904. /* Internal data, OK to advance */
  4905. rb_advance_iter(iter);
  4906. goto again;
  4907. case RINGBUF_TYPE_DATA:
  4908. if (ts && !(*ts)) {
  4909. *ts = iter->read_stamp + event->time_delta;
  4910. ring_buffer_normalize_time_stamp(buffer,
  4911. cpu_buffer->cpu, ts);
  4912. }
  4913. return event;
  4914. default:
  4915. RB_WARN_ON(cpu_buffer, 1);
  4916. }
  4917. return NULL;
  4918. }
  4919. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  4920. static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
  4921. {
  4922. if (likely(!in_nmi())) {
  4923. raw_spin_lock(&cpu_buffer->reader_lock);
  4924. return true;
  4925. }
  4926. /*
  4927. * If an NMI die dumps out the content of the ring buffer
  4928. * trylock must be used to prevent a deadlock if the NMI
  4929. * preempted a task that holds the ring buffer locks. If
  4930. * we get the lock then all is fine, if not, then continue
  4931. * to do the read, but this can corrupt the ring buffer,
  4932. * so it must be permanently disabled from future writes.
  4933. * Reading from NMI is a oneshot deal.
  4934. */
  4935. if (raw_spin_trylock(&cpu_buffer->reader_lock))
  4936. return true;
  4937. /* Continue without locking, but disable the ring buffer */
  4938. atomic_inc(&cpu_buffer->record_disabled);
  4939. return false;
  4940. }
  4941. static inline void
  4942. rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
  4943. {
  4944. if (likely(locked))
  4945. raw_spin_unlock(&cpu_buffer->reader_lock);
  4946. }
  4947. /**
  4948. * ring_buffer_peek - peek at the next event to be read
  4949. * @buffer: The ring buffer to read
  4950. * @cpu: The cpu to peak at
  4951. * @ts: The timestamp counter of this event.
  4952. * @lost_events: a variable to store if events were lost (may be NULL)
  4953. *
  4954. * This will return the event that will be read next, but does
  4955. * not consume the data.
  4956. */
  4957. struct ring_buffer_event *
  4958. ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
  4959. unsigned long *lost_events)
  4960. {
  4961. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  4962. struct ring_buffer_event *event;
  4963. unsigned long flags;
  4964. bool dolock;
  4965. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4966. return NULL;
  4967. again:
  4968. local_irq_save(flags);
  4969. dolock = rb_reader_lock(cpu_buffer);
  4970. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  4971. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4972. rb_advance_reader(cpu_buffer);
  4973. rb_reader_unlock(cpu_buffer, dolock);
  4974. local_irq_restore(flags);
  4975. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4976. goto again;
  4977. return event;
  4978. }
  4979. /** ring_buffer_iter_dropped - report if there are dropped events
  4980. * @iter: The ring buffer iterator
  4981. *
  4982. * Returns true if there was dropped events since the last peek.
  4983. */
  4984. bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
  4985. {
  4986. bool ret = iter->missed_events != 0;
  4987. iter->missed_events = 0;
  4988. return ret;
  4989. }
  4990. EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
  4991. /**
  4992. * ring_buffer_iter_peek - peek at the next event to be read
  4993. * @iter: The ring buffer iterator
  4994. * @ts: The timestamp counter of this event.
  4995. *
  4996. * This will return the event that will be read next, but does
  4997. * not increment the iterator.
  4998. */
  4999. struct ring_buffer_event *
  5000. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  5001. {
  5002. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  5003. struct ring_buffer_event *event;
  5004. unsigned long flags;
  5005. again:
  5006. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5007. event = rb_iter_peek(iter, ts);
  5008. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5009. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  5010. goto again;
  5011. return event;
  5012. }
  5013. /**
  5014. * ring_buffer_consume - return an event and consume it
  5015. * @buffer: The ring buffer to get the next event from
  5016. * @cpu: the cpu to read the buffer from
  5017. * @ts: a variable to store the timestamp (may be NULL)
  5018. * @lost_events: a variable to store if events were lost (may be NULL)
  5019. *
  5020. * Returns the next event in the ring buffer, and that event is consumed.
  5021. * Meaning, that sequential reads will keep returning a different event,
  5022. * and eventually empty the ring buffer if the producer is slower.
  5023. */
  5024. struct ring_buffer_event *
  5025. ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
  5026. unsigned long *lost_events)
  5027. {
  5028. struct ring_buffer_per_cpu *cpu_buffer;
  5029. struct ring_buffer_event *event = NULL;
  5030. unsigned long flags;
  5031. bool dolock;
  5032. again:
  5033. /* might be called in atomic */
  5034. preempt_disable();
  5035. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5036. goto out;
  5037. cpu_buffer = buffer->buffers[cpu];
  5038. local_irq_save(flags);
  5039. dolock = rb_reader_lock(cpu_buffer);
  5040. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  5041. if (event) {
  5042. cpu_buffer->lost_events = 0;
  5043. rb_advance_reader(cpu_buffer);
  5044. }
  5045. rb_reader_unlock(cpu_buffer, dolock);
  5046. local_irq_restore(flags);
  5047. out:
  5048. preempt_enable();
  5049. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  5050. goto again;
  5051. return event;
  5052. }
  5053. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  5054. /**
  5055. * ring_buffer_read_start - start a non consuming read of the buffer
  5056. * @buffer: The ring buffer to read from
  5057. * @cpu: The cpu buffer to iterate over
  5058. * @flags: gfp flags to use for memory allocation
  5059. *
  5060. * This creates an iterator to allow non-consuming iteration through
  5061. * the buffer. If the buffer is disabled for writing, it will produce
  5062. * the same information each time, but if the buffer is still writing
  5063. * then the first hit of a write will cause the iteration to stop.
  5064. *
  5065. * Must be paired with ring_buffer_read_finish.
  5066. */
  5067. struct ring_buffer_iter *
  5068. ring_buffer_read_start(struct trace_buffer *buffer, int cpu, gfp_t flags)
  5069. {
  5070. struct ring_buffer_per_cpu *cpu_buffer;
  5071. struct ring_buffer_iter *iter;
  5072. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5073. return NULL;
  5074. iter = kzalloc_obj(*iter, flags);
  5075. if (!iter)
  5076. return NULL;
  5077. /* Holds the entire event: data and meta data */
  5078. iter->event_size = buffer->subbuf_size;
  5079. iter->event = kmalloc(iter->event_size, flags);
  5080. if (!iter->event) {
  5081. kfree(iter);
  5082. return NULL;
  5083. }
  5084. cpu_buffer = buffer->buffers[cpu];
  5085. iter->cpu_buffer = cpu_buffer;
  5086. atomic_inc(&cpu_buffer->resize_disabled);
  5087. guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
  5088. arch_spin_lock(&cpu_buffer->lock);
  5089. rb_iter_reset(iter);
  5090. arch_spin_unlock(&cpu_buffer->lock);
  5091. return iter;
  5092. }
  5093. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  5094. /**
  5095. * ring_buffer_read_finish - finish reading the iterator of the buffer
  5096. * @iter: The iterator retrieved by ring_buffer_start
  5097. *
  5098. * This re-enables resizing of the buffer, and frees the iterator.
  5099. */
  5100. void
  5101. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  5102. {
  5103. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  5104. /* Use this opportunity to check the integrity of the ring buffer. */
  5105. rb_check_pages(cpu_buffer);
  5106. atomic_dec(&cpu_buffer->resize_disabled);
  5107. kfree(iter->event);
  5108. kfree(iter);
  5109. }
  5110. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  5111. /**
  5112. * ring_buffer_iter_advance - advance the iterator to the next location
  5113. * @iter: The ring buffer iterator
  5114. *
  5115. * Move the location of the iterator such that the next read will
  5116. * be the next location of the iterator.
  5117. */
  5118. void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
  5119. {
  5120. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  5121. unsigned long flags;
  5122. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5123. rb_advance_iter(iter);
  5124. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5125. }
  5126. EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
  5127. /**
  5128. * ring_buffer_size - return the size of the ring buffer (in bytes)
  5129. * @buffer: The ring buffer.
  5130. * @cpu: The CPU to get ring buffer size from.
  5131. */
  5132. unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
  5133. {
  5134. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5135. return 0;
  5136. return buffer->subbuf_size * buffer->buffers[cpu]->nr_pages;
  5137. }
  5138. EXPORT_SYMBOL_GPL(ring_buffer_size);
  5139. /**
  5140. * ring_buffer_max_event_size - return the max data size of an event
  5141. * @buffer: The ring buffer.
  5142. *
  5143. * Returns the maximum size an event can be.
  5144. */
  5145. unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer)
  5146. {
  5147. /* If abs timestamp is requested, events have a timestamp too */
  5148. if (ring_buffer_time_stamp_abs(buffer))
  5149. return buffer->max_data_size - RB_LEN_TIME_EXTEND;
  5150. return buffer->max_data_size;
  5151. }
  5152. EXPORT_SYMBOL_GPL(ring_buffer_max_event_size);
  5153. static void rb_clear_buffer_page(struct buffer_page *page)
  5154. {
  5155. local_set(&page->write, 0);
  5156. local_set(&page->entries, 0);
  5157. rb_init_page(page->page);
  5158. page->read = 0;
  5159. }
  5160. /*
  5161. * When the buffer is memory mapped to user space, each sub buffer
  5162. * has a unique id that is used by the meta data to tell the user
  5163. * where the current reader page is.
  5164. *
  5165. * For a normal allocated ring buffer, the id is saved in the buffer page
  5166. * id field, and updated via this function.
  5167. *
  5168. * But for a fixed memory mapped buffer, the id is already assigned for
  5169. * fixed memory ordering in the memory layout and can not be used. Instead
  5170. * the index of where the page lies in the memory layout is used.
  5171. *
  5172. * For the normal pages, set the buffer page id with the passed in @id
  5173. * value and return that.
  5174. *
  5175. * For fixed memory mapped pages, get the page index in the memory layout
  5176. * and return that as the id.
  5177. */
  5178. static int rb_page_id(struct ring_buffer_per_cpu *cpu_buffer,
  5179. struct buffer_page *bpage, int id)
  5180. {
  5181. /*
  5182. * For boot buffers, the id is the index,
  5183. * otherwise, set the buffer page with this id
  5184. */
  5185. if (cpu_buffer->ring_meta)
  5186. id = rb_meta_subbuf_idx(cpu_buffer->ring_meta, bpage->page);
  5187. else
  5188. bpage->id = id;
  5189. return id;
  5190. }
  5191. static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5192. {
  5193. struct trace_buffer_meta *meta = cpu_buffer->meta_page;
  5194. if (!meta)
  5195. return;
  5196. meta->reader.read = cpu_buffer->reader_page->read;
  5197. meta->reader.id = rb_page_id(cpu_buffer, cpu_buffer->reader_page,
  5198. cpu_buffer->reader_page->id);
  5199. meta->reader.lost_events = cpu_buffer->lost_events;
  5200. meta->entries = local_read(&cpu_buffer->entries);
  5201. meta->overrun = local_read(&cpu_buffer->overrun);
  5202. meta->read = cpu_buffer->read;
  5203. /* Some archs do not have data cache coherency between kernel and user-space */
  5204. flush_kernel_vmap_range(cpu_buffer->meta_page, PAGE_SIZE);
  5205. }
  5206. static void
  5207. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  5208. {
  5209. struct buffer_page *page;
  5210. rb_head_page_deactivate(cpu_buffer);
  5211. cpu_buffer->head_page
  5212. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  5213. rb_clear_buffer_page(cpu_buffer->head_page);
  5214. list_for_each_entry(page, cpu_buffer->pages, list) {
  5215. rb_clear_buffer_page(page);
  5216. }
  5217. cpu_buffer->tail_page = cpu_buffer->head_page;
  5218. cpu_buffer->commit_page = cpu_buffer->head_page;
  5219. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  5220. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  5221. rb_clear_buffer_page(cpu_buffer->reader_page);
  5222. local_set(&cpu_buffer->entries_bytes, 0);
  5223. local_set(&cpu_buffer->overrun, 0);
  5224. local_set(&cpu_buffer->commit_overrun, 0);
  5225. local_set(&cpu_buffer->dropped_events, 0);
  5226. local_set(&cpu_buffer->entries, 0);
  5227. local_set(&cpu_buffer->committing, 0);
  5228. local_set(&cpu_buffer->commits, 0);
  5229. local_set(&cpu_buffer->pages_touched, 0);
  5230. local_set(&cpu_buffer->pages_lost, 0);
  5231. local_set(&cpu_buffer->pages_read, 0);
  5232. cpu_buffer->last_pages_touch = 0;
  5233. cpu_buffer->shortest_full = 0;
  5234. cpu_buffer->read = 0;
  5235. cpu_buffer->read_bytes = 0;
  5236. rb_time_set(&cpu_buffer->write_stamp, 0);
  5237. rb_time_set(&cpu_buffer->before_stamp, 0);
  5238. memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp));
  5239. cpu_buffer->lost_events = 0;
  5240. cpu_buffer->last_overrun = 0;
  5241. rb_head_page_activate(cpu_buffer);
  5242. cpu_buffer->pages_removed = 0;
  5243. if (cpu_buffer->mapped) {
  5244. rb_update_meta_page(cpu_buffer);
  5245. if (cpu_buffer->ring_meta) {
  5246. struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta;
  5247. meta->commit_buffer = meta->head_buffer;
  5248. }
  5249. }
  5250. }
  5251. /* Must have disabled the cpu buffer then done a synchronize_rcu */
  5252. static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  5253. {
  5254. guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
  5255. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  5256. return;
  5257. arch_spin_lock(&cpu_buffer->lock);
  5258. rb_reset_cpu(cpu_buffer);
  5259. arch_spin_unlock(&cpu_buffer->lock);
  5260. }
  5261. /**
  5262. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  5263. * @buffer: The ring buffer to reset a per cpu buffer of
  5264. * @cpu: The CPU buffer to be reset
  5265. */
  5266. void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
  5267. {
  5268. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  5269. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5270. return;
  5271. /* prevent another thread from changing buffer sizes */
  5272. mutex_lock(&buffer->mutex);
  5273. atomic_inc(&cpu_buffer->resize_disabled);
  5274. atomic_inc(&cpu_buffer->record_disabled);
  5275. /* Make sure all commits have finished */
  5276. synchronize_rcu();
  5277. reset_disabled_cpu_buffer(cpu_buffer);
  5278. atomic_dec(&cpu_buffer->record_disabled);
  5279. atomic_dec(&cpu_buffer->resize_disabled);
  5280. mutex_unlock(&buffer->mutex);
  5281. }
  5282. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  5283. /* Flag to ensure proper resetting of atomic variables */
  5284. #define RESET_BIT (1 << 30)
  5285. /**
  5286. * ring_buffer_reset_online_cpus - reset a ring buffer per CPU buffer
  5287. * @buffer: The ring buffer to reset a per cpu buffer of
  5288. */
  5289. void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
  5290. {
  5291. struct ring_buffer_per_cpu *cpu_buffer;
  5292. int cpu;
  5293. /* prevent another thread from changing buffer sizes */
  5294. mutex_lock(&buffer->mutex);
  5295. for_each_online_buffer_cpu(buffer, cpu) {
  5296. cpu_buffer = buffer->buffers[cpu];
  5297. atomic_add(RESET_BIT, &cpu_buffer->resize_disabled);
  5298. atomic_inc(&cpu_buffer->record_disabled);
  5299. }
  5300. /* Make sure all commits have finished */
  5301. synchronize_rcu();
  5302. for_each_buffer_cpu(buffer, cpu) {
  5303. cpu_buffer = buffer->buffers[cpu];
  5304. /*
  5305. * If a CPU came online during the synchronize_rcu(), then
  5306. * ignore it.
  5307. */
  5308. if (!(atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT))
  5309. continue;
  5310. reset_disabled_cpu_buffer(cpu_buffer);
  5311. atomic_dec(&cpu_buffer->record_disabled);
  5312. atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled);
  5313. }
  5314. mutex_unlock(&buffer->mutex);
  5315. }
  5316. /**
  5317. * ring_buffer_reset - reset a ring buffer
  5318. * @buffer: The ring buffer to reset all cpu buffers
  5319. */
  5320. void ring_buffer_reset(struct trace_buffer *buffer)
  5321. {
  5322. struct ring_buffer_per_cpu *cpu_buffer;
  5323. int cpu;
  5324. /* prevent another thread from changing buffer sizes */
  5325. mutex_lock(&buffer->mutex);
  5326. for_each_buffer_cpu(buffer, cpu) {
  5327. cpu_buffer = buffer->buffers[cpu];
  5328. atomic_inc(&cpu_buffer->resize_disabled);
  5329. atomic_inc(&cpu_buffer->record_disabled);
  5330. }
  5331. /* Make sure all commits have finished */
  5332. synchronize_rcu();
  5333. for_each_buffer_cpu(buffer, cpu) {
  5334. cpu_buffer = buffer->buffers[cpu];
  5335. reset_disabled_cpu_buffer(cpu_buffer);
  5336. atomic_dec(&cpu_buffer->record_disabled);
  5337. atomic_dec(&cpu_buffer->resize_disabled);
  5338. }
  5339. mutex_unlock(&buffer->mutex);
  5340. }
  5341. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  5342. /**
  5343. * ring_buffer_empty - is the ring buffer empty?
  5344. * @buffer: The ring buffer to test
  5345. */
  5346. bool ring_buffer_empty(struct trace_buffer *buffer)
  5347. {
  5348. struct ring_buffer_per_cpu *cpu_buffer;
  5349. unsigned long flags;
  5350. bool dolock;
  5351. bool ret;
  5352. int cpu;
  5353. /* yes this is racy, but if you don't like the race, lock the buffer */
  5354. for_each_buffer_cpu(buffer, cpu) {
  5355. cpu_buffer = buffer->buffers[cpu];
  5356. local_irq_save(flags);
  5357. dolock = rb_reader_lock(cpu_buffer);
  5358. ret = rb_per_cpu_empty(cpu_buffer);
  5359. rb_reader_unlock(cpu_buffer, dolock);
  5360. local_irq_restore(flags);
  5361. if (!ret)
  5362. return false;
  5363. }
  5364. return true;
  5365. }
  5366. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  5367. /**
  5368. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  5369. * @buffer: The ring buffer
  5370. * @cpu: The CPU buffer to test
  5371. */
  5372. bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
  5373. {
  5374. struct ring_buffer_per_cpu *cpu_buffer;
  5375. unsigned long flags;
  5376. bool dolock;
  5377. bool ret;
  5378. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5379. return true;
  5380. cpu_buffer = buffer->buffers[cpu];
  5381. local_irq_save(flags);
  5382. dolock = rb_reader_lock(cpu_buffer);
  5383. ret = rb_per_cpu_empty(cpu_buffer);
  5384. rb_reader_unlock(cpu_buffer, dolock);
  5385. local_irq_restore(flags);
  5386. return ret;
  5387. }
  5388. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  5389. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  5390. /**
  5391. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  5392. * @buffer_a: One buffer to swap with
  5393. * @buffer_b: The other buffer to swap with
  5394. * @cpu: the CPU of the buffers to swap
  5395. *
  5396. * This function is useful for tracers that want to take a "snapshot"
  5397. * of a CPU buffer and has another back up buffer lying around.
  5398. * it is expected that the tracer handles the cpu buffer not being
  5399. * used at the moment.
  5400. */
  5401. int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
  5402. struct trace_buffer *buffer_b, int cpu)
  5403. {
  5404. struct ring_buffer_per_cpu *cpu_buffer_a;
  5405. struct ring_buffer_per_cpu *cpu_buffer_b;
  5406. int ret = -EINVAL;
  5407. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  5408. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  5409. return -EINVAL;
  5410. cpu_buffer_a = buffer_a->buffers[cpu];
  5411. cpu_buffer_b = buffer_b->buffers[cpu];
  5412. /* It's up to the callers to not try to swap mapped buffers */
  5413. if (WARN_ON_ONCE(cpu_buffer_a->mapped || cpu_buffer_b->mapped))
  5414. return -EBUSY;
  5415. /* At least make sure the two buffers are somewhat the same */
  5416. if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
  5417. return -EINVAL;
  5418. if (buffer_a->subbuf_order != buffer_b->subbuf_order)
  5419. return -EINVAL;
  5420. if (atomic_read(&buffer_a->record_disabled))
  5421. return -EAGAIN;
  5422. if (atomic_read(&buffer_b->record_disabled))
  5423. return -EAGAIN;
  5424. if (atomic_read(&cpu_buffer_a->record_disabled))
  5425. return -EAGAIN;
  5426. if (atomic_read(&cpu_buffer_b->record_disabled))
  5427. return -EAGAIN;
  5428. /*
  5429. * We can't do a synchronize_rcu here because this
  5430. * function can be called in atomic context.
  5431. * Normally this will be called from the same CPU as cpu.
  5432. * If not it's up to the caller to protect this.
  5433. */
  5434. atomic_inc(&cpu_buffer_a->record_disabled);
  5435. atomic_inc(&cpu_buffer_b->record_disabled);
  5436. ret = -EBUSY;
  5437. if (local_read(&cpu_buffer_a->committing))
  5438. goto out_dec;
  5439. if (local_read(&cpu_buffer_b->committing))
  5440. goto out_dec;
  5441. /*
  5442. * When resize is in progress, we cannot swap it because
  5443. * it will mess the state of the cpu buffer.
  5444. */
  5445. if (atomic_read(&buffer_a->resizing))
  5446. goto out_dec;
  5447. if (atomic_read(&buffer_b->resizing))
  5448. goto out_dec;
  5449. buffer_a->buffers[cpu] = cpu_buffer_b;
  5450. buffer_b->buffers[cpu] = cpu_buffer_a;
  5451. cpu_buffer_b->buffer = buffer_a;
  5452. cpu_buffer_a->buffer = buffer_b;
  5453. ret = 0;
  5454. out_dec:
  5455. atomic_dec(&cpu_buffer_a->record_disabled);
  5456. atomic_dec(&cpu_buffer_b->record_disabled);
  5457. return ret;
  5458. }
  5459. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  5460. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  5461. /**
  5462. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  5463. * @buffer: the buffer to allocate for.
  5464. * @cpu: the cpu buffer to allocate.
  5465. *
  5466. * This function is used in conjunction with ring_buffer_read_page.
  5467. * When reading a full page from the ring buffer, these functions
  5468. * can be used to speed up the process. The calling function should
  5469. * allocate a few pages first with this function. Then when it
  5470. * needs to get pages from the ring buffer, it passes the result
  5471. * of this function into ring_buffer_read_page, which will swap
  5472. * the page that was allocated, with the read page of the buffer.
  5473. *
  5474. * Returns:
  5475. * The page allocated, or ERR_PTR
  5476. */
  5477. struct buffer_data_read_page *
  5478. ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
  5479. {
  5480. struct ring_buffer_per_cpu *cpu_buffer;
  5481. struct buffer_data_read_page *bpage = NULL;
  5482. unsigned long flags;
  5483. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5484. return ERR_PTR(-ENODEV);
  5485. bpage = kzalloc_obj(*bpage);
  5486. if (!bpage)
  5487. return ERR_PTR(-ENOMEM);
  5488. bpage->order = buffer->subbuf_order;
  5489. cpu_buffer = buffer->buffers[cpu];
  5490. local_irq_save(flags);
  5491. arch_spin_lock(&cpu_buffer->lock);
  5492. if (cpu_buffer->free_page) {
  5493. bpage->data = cpu_buffer->free_page;
  5494. cpu_buffer->free_page = NULL;
  5495. }
  5496. arch_spin_unlock(&cpu_buffer->lock);
  5497. local_irq_restore(flags);
  5498. if (bpage->data) {
  5499. rb_init_page(bpage->data);
  5500. } else {
  5501. bpage->data = alloc_cpu_data(cpu, cpu_buffer->buffer->subbuf_order);
  5502. if (!bpage->data) {
  5503. kfree(bpage);
  5504. return ERR_PTR(-ENOMEM);
  5505. }
  5506. }
  5507. return bpage;
  5508. }
  5509. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  5510. /**
  5511. * ring_buffer_free_read_page - free an allocated read page
  5512. * @buffer: the buffer the page was allocate for
  5513. * @cpu: the cpu buffer the page came from
  5514. * @data_page: the page to free
  5515. *
  5516. * Free a page allocated from ring_buffer_alloc_read_page.
  5517. */
  5518. void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu,
  5519. struct buffer_data_read_page *data_page)
  5520. {
  5521. struct ring_buffer_per_cpu *cpu_buffer;
  5522. struct buffer_data_page *bpage = data_page->data;
  5523. struct page *page = virt_to_page(bpage);
  5524. unsigned long flags;
  5525. if (!buffer || !buffer->buffers || !buffer->buffers[cpu])
  5526. return;
  5527. cpu_buffer = buffer->buffers[cpu];
  5528. /*
  5529. * If the page is still in use someplace else, or order of the page
  5530. * is different from the subbuffer order of the buffer -
  5531. * we can't reuse it
  5532. */
  5533. if (page_ref_count(page) > 1 || data_page->order != buffer->subbuf_order)
  5534. goto out;
  5535. local_irq_save(flags);
  5536. arch_spin_lock(&cpu_buffer->lock);
  5537. if (!cpu_buffer->free_page) {
  5538. cpu_buffer->free_page = bpage;
  5539. bpage = NULL;
  5540. }
  5541. arch_spin_unlock(&cpu_buffer->lock);
  5542. local_irq_restore(flags);
  5543. out:
  5544. free_pages((unsigned long)bpage, data_page->order);
  5545. kfree(data_page);
  5546. }
  5547. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  5548. /**
  5549. * ring_buffer_read_page - extract a page from the ring buffer
  5550. * @buffer: buffer to extract from
  5551. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  5552. * @len: amount to extract
  5553. * @cpu: the cpu of the buffer to extract
  5554. * @full: should the extraction only happen when the page is full.
  5555. *
  5556. * This function will pull out a page from the ring buffer and consume it.
  5557. * @data_page must be the address of the variable that was returned
  5558. * from ring_buffer_alloc_read_page. This is because the page might be used
  5559. * to swap with a page in the ring buffer.
  5560. *
  5561. * for example:
  5562. * rpage = ring_buffer_alloc_read_page(buffer, cpu);
  5563. * if (IS_ERR(rpage))
  5564. * return PTR_ERR(rpage);
  5565. * ret = ring_buffer_read_page(buffer, rpage, len, cpu, 0);
  5566. * if (ret >= 0)
  5567. * process_page(ring_buffer_read_page_data(rpage), ret);
  5568. * ring_buffer_free_read_page(buffer, cpu, rpage);
  5569. *
  5570. * When @full is set, the function will not return true unless
  5571. * the writer is off the reader page.
  5572. *
  5573. * Note: it is up to the calling functions to handle sleeps and wakeups.
  5574. * The ring buffer can be used anywhere in the kernel and can not
  5575. * blindly call wake_up. The layer that uses the ring buffer must be
  5576. * responsible for that.
  5577. *
  5578. * Returns:
  5579. * >=0 if data has been transferred, returns the offset of consumed data.
  5580. * <0 if no data has been transferred.
  5581. */
  5582. int ring_buffer_read_page(struct trace_buffer *buffer,
  5583. struct buffer_data_read_page *data_page,
  5584. size_t len, int cpu, int full)
  5585. {
  5586. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  5587. struct ring_buffer_event *event;
  5588. struct buffer_data_page *bpage;
  5589. struct buffer_page *reader;
  5590. unsigned long missed_events;
  5591. unsigned int commit;
  5592. unsigned int read;
  5593. u64 save_timestamp;
  5594. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5595. return -1;
  5596. /*
  5597. * If len is not big enough to hold the page header, then
  5598. * we can not copy anything.
  5599. */
  5600. if (len <= BUF_PAGE_HDR_SIZE)
  5601. return -1;
  5602. len -= BUF_PAGE_HDR_SIZE;
  5603. if (!data_page || !data_page->data)
  5604. return -1;
  5605. if (data_page->order != buffer->subbuf_order)
  5606. return -1;
  5607. bpage = data_page->data;
  5608. if (!bpage)
  5609. return -1;
  5610. guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
  5611. reader = rb_get_reader_page(cpu_buffer);
  5612. if (!reader)
  5613. return -1;
  5614. event = rb_reader_event(cpu_buffer);
  5615. read = reader->read;
  5616. commit = rb_page_size(reader);
  5617. /* Check if any events were dropped */
  5618. missed_events = cpu_buffer->lost_events;
  5619. /*
  5620. * If this page has been partially read or
  5621. * if len is not big enough to read the rest of the page or
  5622. * a writer is still on the page, then
  5623. * we must copy the data from the page to the buffer.
  5624. * Otherwise, we can simply swap the page with the one passed in.
  5625. */
  5626. if (read || (len < (commit - read)) ||
  5627. cpu_buffer->reader_page == cpu_buffer->commit_page ||
  5628. cpu_buffer->mapped) {
  5629. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  5630. unsigned int rpos = read;
  5631. unsigned int pos = 0;
  5632. unsigned int size;
  5633. /*
  5634. * If a full page is expected, this can still be returned
  5635. * if there's been a previous partial read and the
  5636. * rest of the page can be read and the commit page is off
  5637. * the reader page.
  5638. */
  5639. if (full &&
  5640. (!read || (len < (commit - read)) ||
  5641. cpu_buffer->reader_page == cpu_buffer->commit_page))
  5642. return -1;
  5643. if (len > (commit - read))
  5644. len = (commit - read);
  5645. /* Always keep the time extend and data together */
  5646. size = rb_event_ts_length(event);
  5647. if (len < size)
  5648. return -1;
  5649. /* save the current timestamp, since the user will need it */
  5650. save_timestamp = cpu_buffer->read_stamp;
  5651. /* Need to copy one event at a time */
  5652. do {
  5653. /* We need the size of one event, because
  5654. * rb_advance_reader only advances by one event,
  5655. * whereas rb_event_ts_length may include the size of
  5656. * one or two events.
  5657. * We have already ensured there's enough space if this
  5658. * is a time extend. */
  5659. size = rb_event_length(event);
  5660. memcpy(bpage->data + pos, rpage->data + rpos, size);
  5661. len -= size;
  5662. rb_advance_reader(cpu_buffer);
  5663. rpos = reader->read;
  5664. pos += size;
  5665. if (rpos >= commit)
  5666. break;
  5667. event = rb_reader_event(cpu_buffer);
  5668. /* Always keep the time extend and data together */
  5669. size = rb_event_ts_length(event);
  5670. } while (len >= size);
  5671. /* update bpage */
  5672. local_set(&bpage->commit, pos);
  5673. bpage->time_stamp = save_timestamp;
  5674. /* we copied everything to the beginning */
  5675. read = 0;
  5676. } else {
  5677. /* update the entry counter */
  5678. cpu_buffer->read += rb_page_entries(reader);
  5679. cpu_buffer->read_bytes += rb_page_size(reader);
  5680. /* swap the pages */
  5681. rb_init_page(bpage);
  5682. bpage = reader->page;
  5683. reader->page = data_page->data;
  5684. local_set(&reader->write, 0);
  5685. local_set(&reader->entries, 0);
  5686. reader->read = 0;
  5687. data_page->data = bpage;
  5688. /*
  5689. * Use the real_end for the data size,
  5690. * This gives us a chance to store the lost events
  5691. * on the page.
  5692. */
  5693. if (reader->real_end)
  5694. local_set(&bpage->commit, reader->real_end);
  5695. }
  5696. cpu_buffer->lost_events = 0;
  5697. commit = local_read(&bpage->commit);
  5698. /*
  5699. * Set a flag in the commit field if we lost events
  5700. */
  5701. if (missed_events) {
  5702. /* If there is room at the end of the page to save the
  5703. * missed events, then record it there.
  5704. */
  5705. if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
  5706. memcpy(&bpage->data[commit], &missed_events,
  5707. sizeof(missed_events));
  5708. local_add(RB_MISSED_STORED, &bpage->commit);
  5709. commit += sizeof(missed_events);
  5710. }
  5711. local_add(RB_MISSED_EVENTS, &bpage->commit);
  5712. }
  5713. /*
  5714. * This page may be off to user land. Zero it out here.
  5715. */
  5716. if (commit < buffer->subbuf_size)
  5717. memset(&bpage->data[commit], 0, buffer->subbuf_size - commit);
  5718. return read;
  5719. }
  5720. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  5721. /**
  5722. * ring_buffer_read_page_data - get pointer to the data in the page.
  5723. * @page: the page to get the data from
  5724. *
  5725. * Returns pointer to the actual data in this page.
  5726. */
  5727. void *ring_buffer_read_page_data(struct buffer_data_read_page *page)
  5728. {
  5729. return page->data;
  5730. }
  5731. EXPORT_SYMBOL_GPL(ring_buffer_read_page_data);
  5732. /**
  5733. * ring_buffer_subbuf_size_get - get size of the sub buffer.
  5734. * @buffer: the buffer to get the sub buffer size from
  5735. *
  5736. * Returns size of the sub buffer, in bytes.
  5737. */
  5738. int ring_buffer_subbuf_size_get(struct trace_buffer *buffer)
  5739. {
  5740. return buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
  5741. }
  5742. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_size_get);
  5743. /**
  5744. * ring_buffer_subbuf_order_get - get order of system sub pages in one buffer page.
  5745. * @buffer: The ring_buffer to get the system sub page order from
  5746. *
  5747. * By default, one ring buffer sub page equals to one system page. This parameter
  5748. * is configurable, per ring buffer. The size of the ring buffer sub page can be
  5749. * extended, but must be an order of system page size.
  5750. *
  5751. * Returns the order of buffer sub page size, in system pages:
  5752. * 0 means the sub buffer size is 1 system page and so forth.
  5753. * In case of an error < 0 is returned.
  5754. */
  5755. int ring_buffer_subbuf_order_get(struct trace_buffer *buffer)
  5756. {
  5757. if (!buffer)
  5758. return -EINVAL;
  5759. return buffer->subbuf_order;
  5760. }
  5761. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_get);
  5762. /**
  5763. * ring_buffer_subbuf_order_set - set the size of ring buffer sub page.
  5764. * @buffer: The ring_buffer to set the new page size.
  5765. * @order: Order of the system pages in one sub buffer page
  5766. *
  5767. * By default, one ring buffer pages equals to one system page. This API can be
  5768. * used to set new size of the ring buffer page. The size must be order of
  5769. * system page size, that's why the input parameter @order is the order of
  5770. * system pages that are allocated for one ring buffer page:
  5771. * 0 - 1 system page
  5772. * 1 - 2 system pages
  5773. * 3 - 4 system pages
  5774. * ...
  5775. *
  5776. * Returns 0 on success or < 0 in case of an error.
  5777. */
  5778. int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
  5779. {
  5780. struct ring_buffer_per_cpu *cpu_buffer;
  5781. struct buffer_page *bpage, *tmp;
  5782. int old_order, old_size;
  5783. int nr_pages;
  5784. int psize;
  5785. int err;
  5786. int cpu;
  5787. if (!buffer || order < 0)
  5788. return -EINVAL;
  5789. if (buffer->subbuf_order == order)
  5790. return 0;
  5791. psize = (1 << order) * PAGE_SIZE;
  5792. if (psize <= BUF_PAGE_HDR_SIZE)
  5793. return -EINVAL;
  5794. /* Size of a subbuf cannot be greater than the write counter */
  5795. if (psize > RB_WRITE_MASK + 1)
  5796. return -EINVAL;
  5797. old_order = buffer->subbuf_order;
  5798. old_size = buffer->subbuf_size;
  5799. /* prevent another thread from changing buffer sizes */
  5800. guard(mutex)(&buffer->mutex);
  5801. atomic_inc(&buffer->record_disabled);
  5802. /* Make sure all commits have finished */
  5803. synchronize_rcu();
  5804. buffer->subbuf_order = order;
  5805. buffer->subbuf_size = psize - BUF_PAGE_HDR_SIZE;
  5806. /* Make sure all new buffers are allocated, before deleting the old ones */
  5807. for_each_buffer_cpu(buffer, cpu) {
  5808. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5809. continue;
  5810. cpu_buffer = buffer->buffers[cpu];
  5811. if (cpu_buffer->mapped) {
  5812. err = -EBUSY;
  5813. goto error;
  5814. }
  5815. /* Update the number of pages to match the new size */
  5816. nr_pages = old_size * buffer->buffers[cpu]->nr_pages;
  5817. nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size);
  5818. /* we need a minimum of two pages */
  5819. if (nr_pages < 2)
  5820. nr_pages = 2;
  5821. cpu_buffer->nr_pages_to_update = nr_pages;
  5822. /* Include the reader page */
  5823. nr_pages++;
  5824. /* Allocate the new size buffer */
  5825. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  5826. if (__rb_allocate_pages(cpu_buffer, nr_pages,
  5827. &cpu_buffer->new_pages)) {
  5828. /* not enough memory for new pages */
  5829. err = -ENOMEM;
  5830. goto error;
  5831. }
  5832. }
  5833. for_each_buffer_cpu(buffer, cpu) {
  5834. struct buffer_data_page *old_free_data_page;
  5835. struct list_head old_pages;
  5836. unsigned long flags;
  5837. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5838. continue;
  5839. cpu_buffer = buffer->buffers[cpu];
  5840. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5841. /* Clear the head bit to make the link list normal to read */
  5842. rb_head_page_deactivate(cpu_buffer);
  5843. /*
  5844. * Collect buffers from the cpu_buffer pages list and the
  5845. * reader_page on old_pages, so they can be freed later when not
  5846. * under a spinlock. The pages list is a linked list with no
  5847. * head, adding old_pages turns it into a regular list with
  5848. * old_pages being the head.
  5849. */
  5850. list_add(&old_pages, cpu_buffer->pages);
  5851. list_add(&cpu_buffer->reader_page->list, &old_pages);
  5852. /* One page was allocated for the reader page */
  5853. cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next,
  5854. struct buffer_page, list);
  5855. list_del_init(&cpu_buffer->reader_page->list);
  5856. /* Install the new pages, remove the head from the list */
  5857. cpu_buffer->pages = cpu_buffer->new_pages.next;
  5858. list_del_init(&cpu_buffer->new_pages);
  5859. cpu_buffer->cnt++;
  5860. cpu_buffer->head_page
  5861. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  5862. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  5863. cpu_buffer->nr_pages = cpu_buffer->nr_pages_to_update;
  5864. cpu_buffer->nr_pages_to_update = 0;
  5865. old_free_data_page = cpu_buffer->free_page;
  5866. cpu_buffer->free_page = NULL;
  5867. rb_head_page_activate(cpu_buffer);
  5868. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5869. /* Free old sub buffers */
  5870. list_for_each_entry_safe(bpage, tmp, &old_pages, list) {
  5871. list_del_init(&bpage->list);
  5872. free_buffer_page(bpage);
  5873. }
  5874. free_pages((unsigned long)old_free_data_page, old_order);
  5875. rb_check_pages(cpu_buffer);
  5876. }
  5877. atomic_dec(&buffer->record_disabled);
  5878. return 0;
  5879. error:
  5880. buffer->subbuf_order = old_order;
  5881. buffer->subbuf_size = old_size;
  5882. atomic_dec(&buffer->record_disabled);
  5883. for_each_buffer_cpu(buffer, cpu) {
  5884. cpu_buffer = buffer->buffers[cpu];
  5885. if (!cpu_buffer->nr_pages_to_update)
  5886. continue;
  5887. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, list) {
  5888. list_del_init(&bpage->list);
  5889. free_buffer_page(bpage);
  5890. }
  5891. }
  5892. return err;
  5893. }
  5894. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set);
  5895. static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5896. {
  5897. struct page *page;
  5898. if (cpu_buffer->meta_page)
  5899. return 0;
  5900. page = alloc_page(GFP_USER | __GFP_ZERO);
  5901. if (!page)
  5902. return -ENOMEM;
  5903. cpu_buffer->meta_page = page_to_virt(page);
  5904. return 0;
  5905. }
  5906. static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5907. {
  5908. unsigned long addr = (unsigned long)cpu_buffer->meta_page;
  5909. free_page(addr);
  5910. cpu_buffer->meta_page = NULL;
  5911. }
  5912. static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer,
  5913. unsigned long *subbuf_ids)
  5914. {
  5915. struct trace_buffer_meta *meta = cpu_buffer->meta_page;
  5916. unsigned int nr_subbufs = cpu_buffer->nr_pages + 1;
  5917. struct buffer_page *first_subbuf, *subbuf;
  5918. int cnt = 0;
  5919. int id = 0;
  5920. id = rb_page_id(cpu_buffer, cpu_buffer->reader_page, id);
  5921. subbuf_ids[id++] = (unsigned long)cpu_buffer->reader_page->page;
  5922. cnt++;
  5923. first_subbuf = subbuf = rb_set_head_page(cpu_buffer);
  5924. do {
  5925. id = rb_page_id(cpu_buffer, subbuf, id);
  5926. if (WARN_ON(id >= nr_subbufs))
  5927. break;
  5928. subbuf_ids[id] = (unsigned long)subbuf->page;
  5929. rb_inc_page(&subbuf);
  5930. id++;
  5931. cnt++;
  5932. } while (subbuf != first_subbuf);
  5933. WARN_ON(cnt != nr_subbufs);
  5934. /* install subbuf ID to kern VA translation */
  5935. cpu_buffer->subbuf_ids = subbuf_ids;
  5936. meta->meta_struct_len = sizeof(*meta);
  5937. meta->nr_subbufs = nr_subbufs;
  5938. meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
  5939. meta->meta_page_size = meta->subbuf_size;
  5940. rb_update_meta_page(cpu_buffer);
  5941. }
  5942. static struct ring_buffer_per_cpu *
  5943. rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu)
  5944. {
  5945. struct ring_buffer_per_cpu *cpu_buffer;
  5946. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5947. return ERR_PTR(-EINVAL);
  5948. cpu_buffer = buffer->buffers[cpu];
  5949. mutex_lock(&cpu_buffer->mapping_lock);
  5950. if (!cpu_buffer->user_mapped) {
  5951. mutex_unlock(&cpu_buffer->mapping_lock);
  5952. return ERR_PTR(-ENODEV);
  5953. }
  5954. return cpu_buffer;
  5955. }
  5956. static void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  5957. {
  5958. mutex_unlock(&cpu_buffer->mapping_lock);
  5959. }
  5960. /*
  5961. * Fast-path for rb_buffer_(un)map(). Called whenever the meta-page doesn't need
  5962. * to be set-up or torn-down.
  5963. */
  5964. static int __rb_inc_dec_mapped(struct ring_buffer_per_cpu *cpu_buffer,
  5965. bool inc)
  5966. {
  5967. unsigned long flags;
  5968. lockdep_assert_held(&cpu_buffer->mapping_lock);
  5969. /* mapped is always greater or equal to user_mapped */
  5970. if (WARN_ON(cpu_buffer->mapped < cpu_buffer->user_mapped))
  5971. return -EINVAL;
  5972. if (inc && cpu_buffer->mapped == UINT_MAX)
  5973. return -EBUSY;
  5974. if (WARN_ON(!inc && cpu_buffer->user_mapped == 0))
  5975. return -EINVAL;
  5976. mutex_lock(&cpu_buffer->buffer->mutex);
  5977. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5978. if (inc) {
  5979. cpu_buffer->user_mapped++;
  5980. cpu_buffer->mapped++;
  5981. } else {
  5982. cpu_buffer->user_mapped--;
  5983. cpu_buffer->mapped--;
  5984. }
  5985. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5986. mutex_unlock(&cpu_buffer->buffer->mutex);
  5987. return 0;
  5988. }
  5989. /*
  5990. * +--------------+ pgoff == 0
  5991. * | meta page |
  5992. * +--------------+ pgoff == 1
  5993. * | subbuffer 0 |
  5994. * | |
  5995. * +--------------+ pgoff == (1 + (1 << subbuf_order))
  5996. * | subbuffer 1 |
  5997. * | |
  5998. * ...
  5999. */
  6000. #ifdef CONFIG_MMU
  6001. static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
  6002. struct vm_area_struct *vma)
  6003. {
  6004. unsigned long nr_subbufs, nr_pages, nr_vma_pages, pgoff = vma->vm_pgoff;
  6005. unsigned int subbuf_pages, subbuf_order;
  6006. struct page **pages __free(kfree) = NULL;
  6007. int p = 0, s = 0;
  6008. int err;
  6009. /* Refuse MP_PRIVATE or writable mappings */
  6010. if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC ||
  6011. !(vma->vm_flags & VM_MAYSHARE))
  6012. return -EPERM;
  6013. subbuf_order = cpu_buffer->buffer->subbuf_order;
  6014. subbuf_pages = 1 << subbuf_order;
  6015. if (subbuf_order && pgoff % subbuf_pages)
  6016. return -EINVAL;
  6017. /*
  6018. * Make sure the mapping cannot become writable later. Also tell the VM
  6019. * to not touch these pages (VM_DONTCOPY | VM_DONTEXPAND).
  6020. */
  6021. vm_flags_mod(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP,
  6022. VM_MAYWRITE);
  6023. lockdep_assert_held(&cpu_buffer->mapping_lock);
  6024. nr_subbufs = cpu_buffer->nr_pages + 1; /* + reader-subbuf */
  6025. nr_pages = ((nr_subbufs + 1) << subbuf_order); /* + meta-page */
  6026. if (nr_pages <= pgoff)
  6027. return -EINVAL;
  6028. nr_pages -= pgoff;
  6029. nr_vma_pages = vma_pages(vma);
  6030. if (!nr_vma_pages || nr_vma_pages > nr_pages)
  6031. return -EINVAL;
  6032. nr_pages = nr_vma_pages;
  6033. pages = kzalloc_objs(*pages, nr_pages);
  6034. if (!pages)
  6035. return -ENOMEM;
  6036. if (!pgoff) {
  6037. unsigned long meta_page_padding;
  6038. pages[p++] = virt_to_page(cpu_buffer->meta_page);
  6039. /*
  6040. * Pad with the zero-page to align the meta-page with the
  6041. * sub-buffers.
  6042. */
  6043. meta_page_padding = subbuf_pages - 1;
  6044. while (meta_page_padding-- && p < nr_pages) {
  6045. unsigned long __maybe_unused zero_addr =
  6046. vma->vm_start + (PAGE_SIZE * p);
  6047. pages[p++] = ZERO_PAGE(zero_addr);
  6048. }
  6049. } else {
  6050. /* Skip the meta-page */
  6051. pgoff -= subbuf_pages;
  6052. s += pgoff / subbuf_pages;
  6053. }
  6054. while (p < nr_pages) {
  6055. struct page *page;
  6056. int off = 0;
  6057. if (WARN_ON_ONCE(s >= nr_subbufs))
  6058. return -EINVAL;
  6059. page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
  6060. for (; off < (1 << (subbuf_order)); off++, page++) {
  6061. if (p >= nr_pages)
  6062. break;
  6063. pages[p++] = page;
  6064. }
  6065. s++;
  6066. }
  6067. err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
  6068. return err;
  6069. }
  6070. #else
  6071. static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
  6072. struct vm_area_struct *vma)
  6073. {
  6074. return -EOPNOTSUPP;
  6075. }
  6076. #endif
  6077. int ring_buffer_map(struct trace_buffer *buffer, int cpu,
  6078. struct vm_area_struct *vma)
  6079. {
  6080. struct ring_buffer_per_cpu *cpu_buffer;
  6081. unsigned long flags, *subbuf_ids;
  6082. int err;
  6083. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  6084. return -EINVAL;
  6085. cpu_buffer = buffer->buffers[cpu];
  6086. guard(mutex)(&cpu_buffer->mapping_lock);
  6087. if (cpu_buffer->user_mapped) {
  6088. err = __rb_map_vma(cpu_buffer, vma);
  6089. if (!err)
  6090. err = __rb_inc_dec_mapped(cpu_buffer, true);
  6091. return err;
  6092. }
  6093. /* prevent another thread from changing buffer/sub-buffer sizes */
  6094. guard(mutex)(&buffer->mutex);
  6095. err = rb_alloc_meta_page(cpu_buffer);
  6096. if (err)
  6097. return err;
  6098. /* subbuf_ids include the reader while nr_pages does not */
  6099. subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1, sizeof(*subbuf_ids), GFP_KERNEL);
  6100. if (!subbuf_ids) {
  6101. rb_free_meta_page(cpu_buffer);
  6102. return -ENOMEM;
  6103. }
  6104. atomic_inc(&cpu_buffer->resize_disabled);
  6105. /*
  6106. * Lock all readers to block any subbuf swap until the subbuf IDs are
  6107. * assigned.
  6108. */
  6109. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6110. rb_setup_ids_meta_page(cpu_buffer, subbuf_ids);
  6111. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6112. err = __rb_map_vma(cpu_buffer, vma);
  6113. if (!err) {
  6114. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6115. /* This is the first time it is mapped by user */
  6116. cpu_buffer->mapped++;
  6117. cpu_buffer->user_mapped = 1;
  6118. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6119. } else {
  6120. kfree(cpu_buffer->subbuf_ids);
  6121. cpu_buffer->subbuf_ids = NULL;
  6122. rb_free_meta_page(cpu_buffer);
  6123. atomic_dec(&cpu_buffer->resize_disabled);
  6124. }
  6125. return err;
  6126. }
  6127. /*
  6128. * This is called when a VMA is duplicated (e.g., on fork()) to increment
  6129. * the user_mapped counter without remapping pages.
  6130. */
  6131. void ring_buffer_map_dup(struct trace_buffer *buffer, int cpu)
  6132. {
  6133. struct ring_buffer_per_cpu *cpu_buffer;
  6134. if (WARN_ON(!cpumask_test_cpu(cpu, buffer->cpumask)))
  6135. return;
  6136. cpu_buffer = buffer->buffers[cpu];
  6137. guard(mutex)(&cpu_buffer->mapping_lock);
  6138. if (cpu_buffer->user_mapped)
  6139. __rb_inc_dec_mapped(cpu_buffer, true);
  6140. else
  6141. WARN(1, "Unexpected buffer stat, it should be mapped");
  6142. }
  6143. int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
  6144. {
  6145. struct ring_buffer_per_cpu *cpu_buffer;
  6146. unsigned long flags;
  6147. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  6148. return -EINVAL;
  6149. cpu_buffer = buffer->buffers[cpu];
  6150. guard(mutex)(&cpu_buffer->mapping_lock);
  6151. if (!cpu_buffer->user_mapped) {
  6152. return -ENODEV;
  6153. } else if (cpu_buffer->user_mapped > 1) {
  6154. __rb_inc_dec_mapped(cpu_buffer, false);
  6155. return 0;
  6156. }
  6157. guard(mutex)(&buffer->mutex);
  6158. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6159. /* This is the last user space mapping */
  6160. if (!WARN_ON_ONCE(cpu_buffer->mapped < cpu_buffer->user_mapped))
  6161. cpu_buffer->mapped--;
  6162. cpu_buffer->user_mapped = 0;
  6163. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6164. kfree(cpu_buffer->subbuf_ids);
  6165. cpu_buffer->subbuf_ids = NULL;
  6166. rb_free_meta_page(cpu_buffer);
  6167. atomic_dec(&cpu_buffer->resize_disabled);
  6168. return 0;
  6169. }
  6170. int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu)
  6171. {
  6172. struct ring_buffer_per_cpu *cpu_buffer;
  6173. struct buffer_page *reader;
  6174. unsigned long missed_events;
  6175. unsigned long reader_size;
  6176. unsigned long flags;
  6177. cpu_buffer = rb_get_mapped_buffer(buffer, cpu);
  6178. if (IS_ERR(cpu_buffer))
  6179. return (int)PTR_ERR(cpu_buffer);
  6180. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6181. consume:
  6182. if (rb_per_cpu_empty(cpu_buffer))
  6183. goto out;
  6184. reader_size = rb_page_size(cpu_buffer->reader_page);
  6185. /*
  6186. * There are data to be read on the current reader page, we can
  6187. * return to the caller. But before that, we assume the latter will read
  6188. * everything. Let's update the kernel reader accordingly.
  6189. */
  6190. if (cpu_buffer->reader_page->read < reader_size) {
  6191. while (cpu_buffer->reader_page->read < reader_size)
  6192. rb_advance_reader(cpu_buffer);
  6193. goto out;
  6194. }
  6195. /* Did the reader catch up with the writer? */
  6196. if (cpu_buffer->reader_page == cpu_buffer->commit_page)
  6197. goto out;
  6198. reader = rb_get_reader_page(cpu_buffer);
  6199. if (WARN_ON(!reader))
  6200. goto out;
  6201. /* Check if any events were dropped */
  6202. missed_events = cpu_buffer->lost_events;
  6203. if (missed_events) {
  6204. if (cpu_buffer->reader_page != cpu_buffer->commit_page) {
  6205. struct buffer_data_page *bpage = reader->page;
  6206. unsigned int commit;
  6207. /*
  6208. * Use the real_end for the data size,
  6209. * This gives us a chance to store the lost events
  6210. * on the page.
  6211. */
  6212. if (reader->real_end)
  6213. local_set(&bpage->commit, reader->real_end);
  6214. /*
  6215. * If there is room at the end of the page to save the
  6216. * missed events, then record it there.
  6217. */
  6218. commit = rb_page_size(reader);
  6219. if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
  6220. memcpy(&bpage->data[commit], &missed_events,
  6221. sizeof(missed_events));
  6222. local_add(RB_MISSED_STORED, &bpage->commit);
  6223. }
  6224. local_add(RB_MISSED_EVENTS, &bpage->commit);
  6225. } else if (!WARN_ONCE(cpu_buffer->reader_page == cpu_buffer->tail_page,
  6226. "Reader on commit with %ld missed events",
  6227. missed_events)) {
  6228. /*
  6229. * There shouldn't be any missed events if the tail_page
  6230. * is on the reader page. But if the tail page is not on the
  6231. * reader page and the commit_page is, that would mean that
  6232. * there's a commit_overrun (an interrupt preempted an
  6233. * addition of an event and then filled the buffer
  6234. * with new events). In this case it's not an
  6235. * error, but it should still be reported.
  6236. *
  6237. * TODO: Add missed events to the page for user space to know.
  6238. */
  6239. pr_info("Ring buffer [%d] commit overrun lost %ld events at timestamp:%lld\n",
  6240. cpu, missed_events, cpu_buffer->reader_page->page->time_stamp);
  6241. }
  6242. }
  6243. cpu_buffer->lost_events = 0;
  6244. goto consume;
  6245. out:
  6246. /* Some archs do not have data cache coherency between kernel and user-space */
  6247. flush_kernel_vmap_range(cpu_buffer->reader_page->page,
  6248. buffer->subbuf_size + BUF_PAGE_HDR_SIZE);
  6249. rb_update_meta_page(cpu_buffer);
  6250. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6251. rb_put_mapped_buffer(cpu_buffer);
  6252. return 0;
  6253. }
  6254. /*
  6255. * We only allocate new buffers, never free them if the CPU goes down.
  6256. * If we were to free the buffer, then the user would lose any trace that was in
  6257. * the buffer.
  6258. */
  6259. int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
  6260. {
  6261. struct trace_buffer *buffer;
  6262. long nr_pages_same;
  6263. int cpu_i;
  6264. unsigned long nr_pages;
  6265. buffer = container_of(node, struct trace_buffer, node);
  6266. if (cpumask_test_cpu(cpu, buffer->cpumask))
  6267. return 0;
  6268. nr_pages = 0;
  6269. nr_pages_same = 1;
  6270. /* check if all cpu sizes are same */
  6271. for_each_buffer_cpu(buffer, cpu_i) {
  6272. /* fill in the size from first enabled cpu */
  6273. if (nr_pages == 0)
  6274. nr_pages = buffer->buffers[cpu_i]->nr_pages;
  6275. if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
  6276. nr_pages_same = 0;
  6277. break;
  6278. }
  6279. }
  6280. /* allocate minimum pages, user can later expand it */
  6281. if (!nr_pages_same)
  6282. nr_pages = 2;
  6283. buffer->buffers[cpu] =
  6284. rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  6285. if (!buffer->buffers[cpu]) {
  6286. WARN(1, "failed to allocate ring buffer on CPU %u\n",
  6287. cpu);
  6288. return -ENOMEM;
  6289. }
  6290. smp_wmb();
  6291. cpumask_set_cpu(cpu, buffer->cpumask);
  6292. return 0;
  6293. }
  6294. #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
  6295. /*
  6296. * This is a basic integrity check of the ring buffer.
  6297. * Late in the boot cycle this test will run when configured in.
  6298. * It will kick off a thread per CPU that will go into a loop
  6299. * writing to the per cpu ring buffer various sizes of data.
  6300. * Some of the data will be large items, some small.
  6301. *
  6302. * Another thread is created that goes into a spin, sending out
  6303. * IPIs to the other CPUs to also write into the ring buffer.
  6304. * this is to test the nesting ability of the buffer.
  6305. *
  6306. * Basic stats are recorded and reported. If something in the
  6307. * ring buffer should happen that's not expected, a big warning
  6308. * is displayed and all ring buffers are disabled.
  6309. */
  6310. static struct task_struct *rb_threads[NR_CPUS] __initdata;
  6311. struct rb_test_data {
  6312. struct trace_buffer *buffer;
  6313. unsigned long events;
  6314. unsigned long bytes_written;
  6315. unsigned long bytes_alloc;
  6316. unsigned long bytes_dropped;
  6317. unsigned long events_nested;
  6318. unsigned long bytes_written_nested;
  6319. unsigned long bytes_alloc_nested;
  6320. unsigned long bytes_dropped_nested;
  6321. int min_size_nested;
  6322. int max_size_nested;
  6323. int max_size;
  6324. int min_size;
  6325. int cpu;
  6326. int cnt;
  6327. };
  6328. static struct rb_test_data rb_data[NR_CPUS] __initdata;
  6329. /* 1 meg per cpu */
  6330. #define RB_TEST_BUFFER_SIZE 1048576
  6331. static char rb_string[] __initdata =
  6332. "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
  6333. "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
  6334. "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
  6335. static bool rb_test_started __initdata;
  6336. struct rb_item {
  6337. int size;
  6338. char str[];
  6339. };
  6340. static __init int rb_write_something(struct rb_test_data *data, bool nested)
  6341. {
  6342. struct ring_buffer_event *event;
  6343. struct rb_item *item;
  6344. bool started;
  6345. int event_len;
  6346. int size;
  6347. int len;
  6348. int cnt;
  6349. /* Have nested writes different that what is written */
  6350. cnt = data->cnt + (nested ? 27 : 0);
  6351. /* Multiply cnt by ~e, to make some unique increment */
  6352. size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
  6353. len = size + sizeof(struct rb_item);
  6354. started = rb_test_started;
  6355. /* read rb_test_started before checking buffer enabled */
  6356. smp_rmb();
  6357. event = ring_buffer_lock_reserve(data->buffer, len);
  6358. if (!event) {
  6359. /* Ignore dropped events before test starts. */
  6360. if (started) {
  6361. if (nested)
  6362. data->bytes_dropped_nested += len;
  6363. else
  6364. data->bytes_dropped += len;
  6365. }
  6366. return len;
  6367. }
  6368. event_len = ring_buffer_event_length(event);
  6369. if (RB_WARN_ON(data->buffer, event_len < len))
  6370. goto out;
  6371. item = ring_buffer_event_data(event);
  6372. item->size = size;
  6373. memcpy(item->str, rb_string, size);
  6374. if (nested) {
  6375. data->bytes_alloc_nested += event_len;
  6376. data->bytes_written_nested += len;
  6377. data->events_nested++;
  6378. if (!data->min_size_nested || len < data->min_size_nested)
  6379. data->min_size_nested = len;
  6380. if (len > data->max_size_nested)
  6381. data->max_size_nested = len;
  6382. } else {
  6383. data->bytes_alloc += event_len;
  6384. data->bytes_written += len;
  6385. data->events++;
  6386. if (!data->min_size || len < data->min_size)
  6387. data->max_size = len;
  6388. if (len > data->max_size)
  6389. data->max_size = len;
  6390. }
  6391. out:
  6392. ring_buffer_unlock_commit(data->buffer);
  6393. return 0;
  6394. }
  6395. static __init int rb_test(void *arg)
  6396. {
  6397. struct rb_test_data *data = arg;
  6398. while (!kthread_should_stop()) {
  6399. rb_write_something(data, false);
  6400. data->cnt++;
  6401. set_current_state(TASK_INTERRUPTIBLE);
  6402. /* Now sleep between a min of 100-300us and a max of 1ms */
  6403. usleep_range(((data->cnt % 3) + 1) * 100, 1000);
  6404. }
  6405. return 0;
  6406. }
  6407. static __init void rb_ipi(void *ignore)
  6408. {
  6409. struct rb_test_data *data;
  6410. int cpu = smp_processor_id();
  6411. data = &rb_data[cpu];
  6412. rb_write_something(data, true);
  6413. }
  6414. static __init int rb_hammer_test(void *arg)
  6415. {
  6416. while (!kthread_should_stop()) {
  6417. /* Send an IPI to all cpus to write data! */
  6418. smp_call_function(rb_ipi, NULL, 1);
  6419. /* No sleep, but for non preempt, let others run */
  6420. schedule();
  6421. }
  6422. return 0;
  6423. }
  6424. static __init int test_ringbuffer(void)
  6425. {
  6426. struct task_struct *rb_hammer;
  6427. struct trace_buffer *buffer;
  6428. int cpu;
  6429. int ret = 0;
  6430. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  6431. pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
  6432. return 0;
  6433. }
  6434. pr_info("Running ring buffer tests...\n");
  6435. buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
  6436. if (WARN_ON(!buffer))
  6437. return 0;
  6438. /* Disable buffer so that threads can't write to it yet */
  6439. ring_buffer_record_off(buffer);
  6440. for_each_online_cpu(cpu) {
  6441. rb_data[cpu].buffer = buffer;
  6442. rb_data[cpu].cpu = cpu;
  6443. rb_data[cpu].cnt = cpu;
  6444. rb_threads[cpu] = kthread_run_on_cpu(rb_test, &rb_data[cpu],
  6445. cpu, "rbtester/%u");
  6446. if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
  6447. pr_cont("FAILED\n");
  6448. ret = PTR_ERR(rb_threads[cpu]);
  6449. goto out_free;
  6450. }
  6451. }
  6452. /* Now create the rb hammer! */
  6453. rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
  6454. if (WARN_ON(IS_ERR(rb_hammer))) {
  6455. pr_cont("FAILED\n");
  6456. ret = PTR_ERR(rb_hammer);
  6457. goto out_free;
  6458. }
  6459. ring_buffer_record_on(buffer);
  6460. /*
  6461. * Show buffer is enabled before setting rb_test_started.
  6462. * Yes there's a small race window where events could be
  6463. * dropped and the thread won't catch it. But when a ring
  6464. * buffer gets enabled, there will always be some kind of
  6465. * delay before other CPUs see it. Thus, we don't care about
  6466. * those dropped events. We care about events dropped after
  6467. * the threads see that the buffer is active.
  6468. */
  6469. smp_wmb();
  6470. rb_test_started = true;
  6471. set_current_state(TASK_INTERRUPTIBLE);
  6472. /* Just run for 10 seconds */
  6473. schedule_timeout(10 * HZ);
  6474. kthread_stop(rb_hammer);
  6475. out_free:
  6476. for_each_online_cpu(cpu) {
  6477. if (!rb_threads[cpu])
  6478. break;
  6479. kthread_stop(rb_threads[cpu]);
  6480. }
  6481. if (ret) {
  6482. ring_buffer_free(buffer);
  6483. return ret;
  6484. }
  6485. /* Report! */
  6486. pr_info("finished\n");
  6487. for_each_online_cpu(cpu) {
  6488. struct ring_buffer_event *event;
  6489. struct rb_test_data *data = &rb_data[cpu];
  6490. struct rb_item *item;
  6491. unsigned long total_events;
  6492. unsigned long total_dropped;
  6493. unsigned long total_written;
  6494. unsigned long total_alloc;
  6495. unsigned long total_read = 0;
  6496. unsigned long total_size = 0;
  6497. unsigned long total_len = 0;
  6498. unsigned long total_lost = 0;
  6499. unsigned long lost;
  6500. int big_event_size;
  6501. int small_event_size;
  6502. ret = -1;
  6503. total_events = data->events + data->events_nested;
  6504. total_written = data->bytes_written + data->bytes_written_nested;
  6505. total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
  6506. total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
  6507. big_event_size = data->max_size + data->max_size_nested;
  6508. small_event_size = data->min_size + data->min_size_nested;
  6509. pr_info("CPU %d:\n", cpu);
  6510. pr_info(" events: %ld\n", total_events);
  6511. pr_info(" dropped bytes: %ld\n", total_dropped);
  6512. pr_info(" alloced bytes: %ld\n", total_alloc);
  6513. pr_info(" written bytes: %ld\n", total_written);
  6514. pr_info(" biggest event: %d\n", big_event_size);
  6515. pr_info(" smallest event: %d\n", small_event_size);
  6516. if (RB_WARN_ON(buffer, total_dropped))
  6517. break;
  6518. ret = 0;
  6519. while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
  6520. total_lost += lost;
  6521. item = ring_buffer_event_data(event);
  6522. total_len += ring_buffer_event_length(event);
  6523. total_size += item->size + sizeof(struct rb_item);
  6524. if (memcmp(&item->str[0], rb_string, item->size) != 0) {
  6525. pr_info("FAILED!\n");
  6526. pr_info("buffer had: %.*s\n", item->size, item->str);
  6527. pr_info("expected: %.*s\n", item->size, rb_string);
  6528. RB_WARN_ON(buffer, 1);
  6529. ret = -1;
  6530. break;
  6531. }
  6532. total_read++;
  6533. }
  6534. if (ret)
  6535. break;
  6536. ret = -1;
  6537. pr_info(" read events: %ld\n", total_read);
  6538. pr_info(" lost events: %ld\n", total_lost);
  6539. pr_info(" total events: %ld\n", total_lost + total_read);
  6540. pr_info(" recorded len bytes: %ld\n", total_len);
  6541. pr_info(" recorded size bytes: %ld\n", total_size);
  6542. if (total_lost) {
  6543. pr_info(" With dropped events, record len and size may not match\n"
  6544. " alloced and written from above\n");
  6545. } else {
  6546. if (RB_WARN_ON(buffer, total_len != total_alloc ||
  6547. total_size != total_written))
  6548. break;
  6549. }
  6550. if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
  6551. break;
  6552. ret = 0;
  6553. }
  6554. if (!ret)
  6555. pr_info("Ring buffer PASSED!\n");
  6556. ring_buffer_free(buffer);
  6557. return 0;
  6558. }
  6559. late_initcall(test_ringbuffer);
  6560. #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */