ring_buffer.c 203 KB

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