ring_buffer.c 204 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682
  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 both the head and commit are on the reader_page then we are done. */
  1573. if (head_page == cpu_buffer->reader_page &&
  1574. head_page == cpu_buffer->commit_page)
  1575. goto done;
  1576. /* Iterate until finding the commit page */
  1577. for (i = 0; i < meta->nr_subbufs + 1; i++, rb_inc_page(&head_page)) {
  1578. /* Reader page has already been done */
  1579. if (head_page == cpu_buffer->reader_page)
  1580. continue;
  1581. ret = rb_validate_buffer(head_page->page, cpu_buffer->cpu);
  1582. if (ret < 0) {
  1583. pr_info("Ring buffer meta [%d] invalid buffer page\n",
  1584. cpu_buffer->cpu);
  1585. goto invalid;
  1586. }
  1587. /* If the buffer has content, update pages_touched */
  1588. if (ret)
  1589. local_inc(&cpu_buffer->pages_touched);
  1590. entries += ret;
  1591. entry_bytes += local_read(&head_page->page->commit);
  1592. local_set(&cpu_buffer->head_page->entries, ret);
  1593. if (head_page == cpu_buffer->commit_page)
  1594. break;
  1595. }
  1596. if (head_page != cpu_buffer->commit_page) {
  1597. pr_info("Ring buffer meta [%d] commit page not found\n",
  1598. cpu_buffer->cpu);
  1599. goto invalid;
  1600. }
  1601. done:
  1602. local_set(&cpu_buffer->entries, entries);
  1603. local_set(&cpu_buffer->entries_bytes, entry_bytes);
  1604. pr_info("Ring buffer meta [%d] is from previous boot!\n", cpu_buffer->cpu);
  1605. return;
  1606. invalid:
  1607. /* The content of the buffers are invalid, reset the meta data */
  1608. meta->head_buffer = 0;
  1609. meta->commit_buffer = 0;
  1610. /* Reset the reader page */
  1611. local_set(&cpu_buffer->reader_page->entries, 0);
  1612. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1613. /* Reset all the subbuffers */
  1614. for (i = 0; i < meta->nr_subbufs - 1; i++, rb_inc_page(&head_page)) {
  1615. local_set(&head_page->entries, 0);
  1616. local_set(&head_page->page->commit, 0);
  1617. }
  1618. }
  1619. /* Used to calculate data delta */
  1620. static char rb_data_ptr[] = "";
  1621. #define THIS_TEXT_PTR ((unsigned long)rb_meta_init_text_addr)
  1622. #define THIS_DATA_PTR ((unsigned long)rb_data_ptr)
  1623. static void rb_meta_init_text_addr(struct ring_buffer_meta *meta)
  1624. {
  1625. meta->text_addr = THIS_TEXT_PTR;
  1626. meta->data_addr = THIS_DATA_PTR;
  1627. }
  1628. static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages)
  1629. {
  1630. struct ring_buffer_meta *meta;
  1631. unsigned long *subbuf_mask;
  1632. unsigned long delta;
  1633. void *subbuf;
  1634. int cpu;
  1635. int i;
  1636. /* Create a mask to test the subbuf array */
  1637. subbuf_mask = bitmap_alloc(nr_pages + 1, GFP_KERNEL);
  1638. /* If subbuf_mask fails to allocate, then rb_meta_valid() will return false */
  1639. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  1640. void *next_meta;
  1641. meta = rb_range_meta(buffer, nr_pages, cpu);
  1642. if (rb_meta_valid(meta, cpu, buffer, nr_pages, subbuf_mask)) {
  1643. /* Make the mappings match the current address */
  1644. subbuf = rb_subbufs_from_meta(meta);
  1645. delta = (unsigned long)subbuf - meta->first_buffer;
  1646. meta->first_buffer += delta;
  1647. meta->head_buffer += delta;
  1648. meta->commit_buffer += delta;
  1649. buffer->last_text_delta = THIS_TEXT_PTR - meta->text_addr;
  1650. buffer->last_data_delta = THIS_DATA_PTR - meta->data_addr;
  1651. continue;
  1652. }
  1653. if (cpu < nr_cpu_ids - 1)
  1654. next_meta = rb_range_meta(buffer, nr_pages, cpu + 1);
  1655. else
  1656. next_meta = (void *)buffer->range_addr_end;
  1657. memset(meta, 0, next_meta - (void *)meta);
  1658. meta->magic = RING_BUFFER_META_MAGIC;
  1659. meta->struct_size = sizeof(*meta);
  1660. meta->nr_subbufs = nr_pages + 1;
  1661. meta->subbuf_size = PAGE_SIZE;
  1662. subbuf = rb_subbufs_from_meta(meta);
  1663. meta->first_buffer = (unsigned long)subbuf;
  1664. rb_meta_init_text_addr(meta);
  1665. /*
  1666. * The buffers[] array holds the order of the sub-buffers
  1667. * that are after the meta data. The sub-buffers may
  1668. * be swapped out when read and inserted into a different
  1669. * location of the ring buffer. Although their addresses
  1670. * remain the same, the buffers[] array contains the
  1671. * index into the sub-buffers holding their actual order.
  1672. */
  1673. for (i = 0; i < meta->nr_subbufs; i++) {
  1674. meta->buffers[i] = i;
  1675. rb_init_page(subbuf);
  1676. subbuf += meta->subbuf_size;
  1677. }
  1678. }
  1679. bitmap_free(subbuf_mask);
  1680. }
  1681. static void *rbm_start(struct seq_file *m, loff_t *pos)
  1682. {
  1683. struct ring_buffer_per_cpu *cpu_buffer = m->private;
  1684. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  1685. unsigned long val;
  1686. if (!meta)
  1687. return NULL;
  1688. if (*pos > meta->nr_subbufs)
  1689. return NULL;
  1690. val = *pos;
  1691. val++;
  1692. return (void *)val;
  1693. }
  1694. static void *rbm_next(struct seq_file *m, void *v, loff_t *pos)
  1695. {
  1696. (*pos)++;
  1697. return rbm_start(m, pos);
  1698. }
  1699. static int rbm_show(struct seq_file *m, void *v)
  1700. {
  1701. struct ring_buffer_per_cpu *cpu_buffer = m->private;
  1702. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  1703. unsigned long val = (unsigned long)v;
  1704. if (val == 1) {
  1705. seq_printf(m, "head_buffer: %d\n",
  1706. rb_meta_subbuf_idx(meta, (void *)meta->head_buffer));
  1707. seq_printf(m, "commit_buffer: %d\n",
  1708. rb_meta_subbuf_idx(meta, (void *)meta->commit_buffer));
  1709. seq_printf(m, "subbuf_size: %d\n", meta->subbuf_size);
  1710. seq_printf(m, "nr_subbufs: %d\n", meta->nr_subbufs);
  1711. return 0;
  1712. }
  1713. val -= 2;
  1714. seq_printf(m, "buffer[%ld]: %d\n", val, meta->buffers[val]);
  1715. return 0;
  1716. }
  1717. static void rbm_stop(struct seq_file *m, void *p)
  1718. {
  1719. }
  1720. static const struct seq_operations rb_meta_seq_ops = {
  1721. .start = rbm_start,
  1722. .next = rbm_next,
  1723. .show = rbm_show,
  1724. .stop = rbm_stop,
  1725. };
  1726. int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, int cpu)
  1727. {
  1728. struct seq_file *m;
  1729. int ret;
  1730. ret = seq_open(file, &rb_meta_seq_ops);
  1731. if (ret)
  1732. return ret;
  1733. m = file->private_data;
  1734. m->private = buffer->buffers[cpu];
  1735. return 0;
  1736. }
  1737. /* Map the buffer_pages to the previous head and commit pages */
  1738. static void rb_meta_buffer_update(struct ring_buffer_per_cpu *cpu_buffer,
  1739. struct buffer_page *bpage)
  1740. {
  1741. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  1742. if (meta->head_buffer == (unsigned long)bpage->page)
  1743. cpu_buffer->head_page = bpage;
  1744. if (meta->commit_buffer == (unsigned long)bpage->page) {
  1745. cpu_buffer->commit_page = bpage;
  1746. cpu_buffer->tail_page = bpage;
  1747. }
  1748. }
  1749. static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1750. long nr_pages, struct list_head *pages)
  1751. {
  1752. struct trace_buffer *buffer = cpu_buffer->buffer;
  1753. struct ring_buffer_meta *meta = NULL;
  1754. struct buffer_page *bpage, *tmp;
  1755. bool user_thread = current->mm != NULL;
  1756. gfp_t mflags;
  1757. long i;
  1758. /*
  1759. * Check if the available memory is there first.
  1760. * Note, si_mem_available() only gives us a rough estimate of available
  1761. * memory. It may not be accurate. But we don't care, we just want
  1762. * to prevent doing any allocation when it is obvious that it is
  1763. * not going to succeed.
  1764. */
  1765. i = si_mem_available();
  1766. if (i < nr_pages)
  1767. return -ENOMEM;
  1768. /*
  1769. * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
  1770. * gracefully without invoking oom-killer and the system is not
  1771. * destabilized.
  1772. */
  1773. mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
  1774. /*
  1775. * If a user thread allocates too much, and si_mem_available()
  1776. * reports there's enough memory, even though there is not.
  1777. * Make sure the OOM killer kills this thread. This can happen
  1778. * even with RETRY_MAYFAIL because another task may be doing
  1779. * an allocation after this task has taken all memory.
  1780. * This is the task the OOM killer needs to take out during this
  1781. * loop, even if it was triggered by an allocation somewhere else.
  1782. */
  1783. if (user_thread)
  1784. set_current_oom_origin();
  1785. if (buffer->range_addr_start)
  1786. meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu);
  1787. for (i = 0; i < nr_pages; i++) {
  1788. struct page *page;
  1789. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  1790. mflags, cpu_to_node(cpu_buffer->cpu));
  1791. if (!bpage)
  1792. goto free_pages;
  1793. rb_check_bpage(cpu_buffer, bpage);
  1794. /*
  1795. * Append the pages as for mapped buffers we want to keep
  1796. * the order
  1797. */
  1798. list_add_tail(&bpage->list, pages);
  1799. if (meta) {
  1800. /* A range was given. Use that for the buffer page */
  1801. bpage->page = rb_range_buffer(cpu_buffer, i + 1);
  1802. if (!bpage->page)
  1803. goto free_pages;
  1804. /* If this is valid from a previous boot */
  1805. if (meta->head_buffer)
  1806. rb_meta_buffer_update(cpu_buffer, bpage);
  1807. bpage->range = 1;
  1808. bpage->id = i + 1;
  1809. } else {
  1810. page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu),
  1811. mflags | __GFP_COMP | __GFP_ZERO,
  1812. cpu_buffer->buffer->subbuf_order);
  1813. if (!page)
  1814. goto free_pages;
  1815. bpage->page = page_address(page);
  1816. rb_init_page(bpage->page);
  1817. }
  1818. bpage->order = cpu_buffer->buffer->subbuf_order;
  1819. if (user_thread && fatal_signal_pending(current))
  1820. goto free_pages;
  1821. }
  1822. if (user_thread)
  1823. clear_current_oom_origin();
  1824. return 0;
  1825. free_pages:
  1826. list_for_each_entry_safe(bpage, tmp, pages, list) {
  1827. list_del_init(&bpage->list);
  1828. free_buffer_page(bpage);
  1829. }
  1830. if (user_thread)
  1831. clear_current_oom_origin();
  1832. return -ENOMEM;
  1833. }
  1834. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1835. unsigned long nr_pages)
  1836. {
  1837. LIST_HEAD(pages);
  1838. WARN_ON(!nr_pages);
  1839. if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
  1840. return -ENOMEM;
  1841. /*
  1842. * The ring buffer page list is a circular list that does not
  1843. * start and end with a list head. All page list items point to
  1844. * other pages.
  1845. */
  1846. cpu_buffer->pages = pages.next;
  1847. list_del(&pages);
  1848. cpu_buffer->nr_pages = nr_pages;
  1849. rb_check_pages(cpu_buffer);
  1850. return 0;
  1851. }
  1852. static struct ring_buffer_per_cpu *
  1853. rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
  1854. {
  1855. struct ring_buffer_per_cpu *cpu_buffer;
  1856. struct ring_buffer_meta *meta;
  1857. struct buffer_page *bpage;
  1858. struct page *page;
  1859. int ret;
  1860. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  1861. GFP_KERNEL, cpu_to_node(cpu));
  1862. if (!cpu_buffer)
  1863. return NULL;
  1864. cpu_buffer->cpu = cpu;
  1865. cpu_buffer->buffer = buffer;
  1866. raw_spin_lock_init(&cpu_buffer->reader_lock);
  1867. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  1868. cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  1869. INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
  1870. init_completion(&cpu_buffer->update_done);
  1871. init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
  1872. init_waitqueue_head(&cpu_buffer->irq_work.waiters);
  1873. init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
  1874. mutex_init(&cpu_buffer->mapping_lock);
  1875. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  1876. GFP_KERNEL, cpu_to_node(cpu));
  1877. if (!bpage)
  1878. goto fail_free_buffer;
  1879. rb_check_bpage(cpu_buffer, bpage);
  1880. cpu_buffer->reader_page = bpage;
  1881. if (buffer->range_addr_start) {
  1882. /*
  1883. * Range mapped buffers have the same restrictions as memory
  1884. * mapped ones do.
  1885. */
  1886. cpu_buffer->mapped = 1;
  1887. cpu_buffer->ring_meta = rb_range_meta(buffer, nr_pages, cpu);
  1888. bpage->page = rb_range_buffer(cpu_buffer, 0);
  1889. if (!bpage->page)
  1890. goto fail_free_reader;
  1891. if (cpu_buffer->ring_meta->head_buffer)
  1892. rb_meta_buffer_update(cpu_buffer, bpage);
  1893. bpage->range = 1;
  1894. } else {
  1895. page = alloc_pages_node(cpu_to_node(cpu),
  1896. GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
  1897. cpu_buffer->buffer->subbuf_order);
  1898. if (!page)
  1899. goto fail_free_reader;
  1900. bpage->page = page_address(page);
  1901. rb_init_page(bpage->page);
  1902. }
  1903. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  1904. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  1905. ret = rb_allocate_pages(cpu_buffer, nr_pages);
  1906. if (ret < 0)
  1907. goto fail_free_reader;
  1908. rb_meta_validate_events(cpu_buffer);
  1909. /* If the boot meta was valid then this has already been updated */
  1910. meta = cpu_buffer->ring_meta;
  1911. if (!meta || !meta->head_buffer ||
  1912. !cpu_buffer->head_page || !cpu_buffer->commit_page || !cpu_buffer->tail_page) {
  1913. if (meta && meta->head_buffer &&
  1914. (cpu_buffer->head_page || cpu_buffer->commit_page || cpu_buffer->tail_page)) {
  1915. pr_warn("Ring buffer meta buffers not all mapped\n");
  1916. if (!cpu_buffer->head_page)
  1917. pr_warn(" Missing head_page\n");
  1918. if (!cpu_buffer->commit_page)
  1919. pr_warn(" Missing commit_page\n");
  1920. if (!cpu_buffer->tail_page)
  1921. pr_warn(" Missing tail_page\n");
  1922. }
  1923. cpu_buffer->head_page
  1924. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  1925. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  1926. rb_head_page_activate(cpu_buffer);
  1927. if (cpu_buffer->ring_meta)
  1928. meta->commit_buffer = meta->head_buffer;
  1929. } else {
  1930. /* The valid meta buffer still needs to activate the head page */
  1931. rb_head_page_activate(cpu_buffer);
  1932. }
  1933. return cpu_buffer;
  1934. fail_free_reader:
  1935. free_buffer_page(cpu_buffer->reader_page);
  1936. fail_free_buffer:
  1937. kfree(cpu_buffer);
  1938. return NULL;
  1939. }
  1940. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  1941. {
  1942. struct list_head *head = cpu_buffer->pages;
  1943. struct buffer_page *bpage, *tmp;
  1944. irq_work_sync(&cpu_buffer->irq_work.work);
  1945. free_buffer_page(cpu_buffer->reader_page);
  1946. if (head) {
  1947. rb_head_page_deactivate(cpu_buffer);
  1948. list_for_each_entry_safe(bpage, tmp, head, list) {
  1949. list_del_init(&bpage->list);
  1950. free_buffer_page(bpage);
  1951. }
  1952. bpage = list_entry(head, struct buffer_page, list);
  1953. free_buffer_page(bpage);
  1954. }
  1955. free_page((unsigned long)cpu_buffer->free_page);
  1956. kfree(cpu_buffer);
  1957. }
  1958. static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
  1959. int order, unsigned long start,
  1960. unsigned long end,
  1961. struct lock_class_key *key)
  1962. {
  1963. struct trace_buffer *buffer;
  1964. long nr_pages;
  1965. int subbuf_size;
  1966. int bsize;
  1967. int cpu;
  1968. int ret;
  1969. /* keep it in its own cache line */
  1970. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  1971. GFP_KERNEL);
  1972. if (!buffer)
  1973. return NULL;
  1974. if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  1975. goto fail_free_buffer;
  1976. buffer->subbuf_order = order;
  1977. subbuf_size = (PAGE_SIZE << order);
  1978. buffer->subbuf_size = subbuf_size - BUF_PAGE_HDR_SIZE;
  1979. /* Max payload is buffer page size - header (8bytes) */
  1980. buffer->max_data_size = buffer->subbuf_size - (sizeof(u32) * 2);
  1981. buffer->flags = flags;
  1982. buffer->clock = trace_clock_local;
  1983. buffer->reader_lock_key = key;
  1984. init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
  1985. init_waitqueue_head(&buffer->irq_work.waiters);
  1986. buffer->cpus = nr_cpu_ids;
  1987. bsize = sizeof(void *) * nr_cpu_ids;
  1988. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  1989. GFP_KERNEL);
  1990. if (!buffer->buffers)
  1991. goto fail_free_cpumask;
  1992. /* If start/end are specified, then that overrides size */
  1993. if (start && end) {
  1994. unsigned long ptr;
  1995. int n;
  1996. size = end - start;
  1997. size = size / nr_cpu_ids;
  1998. /*
  1999. * The number of sub-buffers (nr_pages) is determined by the
  2000. * total size allocated minus the meta data size.
  2001. * Then that is divided by the number of per CPU buffers
  2002. * needed, plus account for the integer array index that
  2003. * will be appended to the meta data.
  2004. */
  2005. nr_pages = (size - sizeof(struct ring_buffer_meta)) /
  2006. (subbuf_size + sizeof(int));
  2007. /* Need at least two pages plus the reader page */
  2008. if (nr_pages < 3)
  2009. goto fail_free_buffers;
  2010. again:
  2011. /* Make sure that the size fits aligned */
  2012. for (n = 0, ptr = start; n < nr_cpu_ids; n++) {
  2013. ptr += sizeof(struct ring_buffer_meta) +
  2014. sizeof(int) * nr_pages;
  2015. ptr = ALIGN(ptr, subbuf_size);
  2016. ptr += subbuf_size * nr_pages;
  2017. }
  2018. if (ptr > end) {
  2019. if (nr_pages <= 3)
  2020. goto fail_free_buffers;
  2021. nr_pages--;
  2022. goto again;
  2023. }
  2024. /* nr_pages should not count the reader page */
  2025. nr_pages--;
  2026. buffer->range_addr_start = start;
  2027. buffer->range_addr_end = end;
  2028. rb_range_meta_init(buffer, nr_pages);
  2029. } else {
  2030. /* need at least two pages */
  2031. nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
  2032. if (nr_pages < 2)
  2033. nr_pages = 2;
  2034. }
  2035. cpu = raw_smp_processor_id();
  2036. cpumask_set_cpu(cpu, buffer->cpumask);
  2037. buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  2038. if (!buffer->buffers[cpu])
  2039. goto fail_free_buffers;
  2040. ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
  2041. if (ret < 0)
  2042. goto fail_free_buffers;
  2043. mutex_init(&buffer->mutex);
  2044. return buffer;
  2045. fail_free_buffers:
  2046. for_each_buffer_cpu(buffer, cpu) {
  2047. if (buffer->buffers[cpu])
  2048. rb_free_cpu_buffer(buffer->buffers[cpu]);
  2049. }
  2050. kfree(buffer->buffers);
  2051. fail_free_cpumask:
  2052. free_cpumask_var(buffer->cpumask);
  2053. fail_free_buffer:
  2054. kfree(buffer);
  2055. return NULL;
  2056. }
  2057. /**
  2058. * __ring_buffer_alloc - allocate a new ring_buffer
  2059. * @size: the size in bytes per cpu that is needed.
  2060. * @flags: attributes to set for the ring buffer.
  2061. * @key: ring buffer reader_lock_key.
  2062. *
  2063. * Currently the only flag that is available is the RB_FL_OVERWRITE
  2064. * flag. This flag means that the buffer will overwrite old data
  2065. * when the buffer wraps. If this flag is not set, the buffer will
  2066. * drop data when the tail hits the head.
  2067. */
  2068. struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  2069. struct lock_class_key *key)
  2070. {
  2071. /* Default buffer page size - one system page */
  2072. return alloc_buffer(size, flags, 0, 0, 0,key);
  2073. }
  2074. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  2075. /**
  2076. * __ring_buffer_alloc_range - allocate a new ring_buffer from existing memory
  2077. * @size: the size in bytes per cpu that is needed.
  2078. * @flags: attributes to set for the ring buffer.
  2079. * @start: start of allocated range
  2080. * @range_size: size of allocated range
  2081. * @order: sub-buffer order
  2082. * @key: ring buffer reader_lock_key.
  2083. *
  2084. * Currently the only flag that is available is the RB_FL_OVERWRITE
  2085. * flag. This flag means that the buffer will overwrite old data
  2086. * when the buffer wraps. If this flag is not set, the buffer will
  2087. * drop data when the tail hits the head.
  2088. */
  2089. struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flags,
  2090. int order, unsigned long start,
  2091. unsigned long range_size,
  2092. struct lock_class_key *key)
  2093. {
  2094. return alloc_buffer(size, flags, order, start, start + range_size, key);
  2095. }
  2096. /**
  2097. * ring_buffer_last_boot_delta - return the delta offset from last boot
  2098. * @buffer: The buffer to return the delta from
  2099. * @text: Return text delta
  2100. * @data: Return data delta
  2101. *
  2102. * Returns: The true if the delta is non zero
  2103. */
  2104. bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, long *text,
  2105. long *data)
  2106. {
  2107. if (!buffer)
  2108. return false;
  2109. if (!buffer->last_text_delta)
  2110. return false;
  2111. *text = buffer->last_text_delta;
  2112. *data = buffer->last_data_delta;
  2113. return true;
  2114. }
  2115. /**
  2116. * ring_buffer_free - free a ring buffer.
  2117. * @buffer: the buffer to free.
  2118. */
  2119. void
  2120. ring_buffer_free(struct trace_buffer *buffer)
  2121. {
  2122. int cpu;
  2123. cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
  2124. irq_work_sync(&buffer->irq_work.work);
  2125. for_each_buffer_cpu(buffer, cpu)
  2126. rb_free_cpu_buffer(buffer->buffers[cpu]);
  2127. kfree(buffer->buffers);
  2128. free_cpumask_var(buffer->cpumask);
  2129. kfree(buffer);
  2130. }
  2131. EXPORT_SYMBOL_GPL(ring_buffer_free);
  2132. void ring_buffer_set_clock(struct trace_buffer *buffer,
  2133. u64 (*clock)(void))
  2134. {
  2135. buffer->clock = clock;
  2136. }
  2137. void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
  2138. {
  2139. buffer->time_stamp_abs = abs;
  2140. }
  2141. bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
  2142. {
  2143. return buffer->time_stamp_abs;
  2144. }
  2145. static inline unsigned long rb_page_entries(struct buffer_page *bpage)
  2146. {
  2147. return local_read(&bpage->entries) & RB_WRITE_MASK;
  2148. }
  2149. static inline unsigned long rb_page_write(struct buffer_page *bpage)
  2150. {
  2151. return local_read(&bpage->write) & RB_WRITE_MASK;
  2152. }
  2153. static bool
  2154. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
  2155. {
  2156. struct list_head *tail_page, *to_remove, *next_page;
  2157. struct buffer_page *to_remove_page, *tmp_iter_page;
  2158. struct buffer_page *last_page, *first_page;
  2159. unsigned long nr_removed;
  2160. unsigned long head_bit;
  2161. int page_entries;
  2162. head_bit = 0;
  2163. raw_spin_lock_irq(&cpu_buffer->reader_lock);
  2164. atomic_inc(&cpu_buffer->record_disabled);
  2165. /*
  2166. * We don't race with the readers since we have acquired the reader
  2167. * lock. We also don't race with writers after disabling recording.
  2168. * This makes it easy to figure out the first and the last page to be
  2169. * removed from the list. We unlink all the pages in between including
  2170. * the first and last pages. This is done in a busy loop so that we
  2171. * lose the least number of traces.
  2172. * The pages are freed after we restart recording and unlock readers.
  2173. */
  2174. tail_page = &cpu_buffer->tail_page->list;
  2175. /*
  2176. * tail page might be on reader page, we remove the next page
  2177. * from the ring buffer
  2178. */
  2179. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  2180. tail_page = rb_list_head(tail_page->next);
  2181. to_remove = tail_page;
  2182. /* start of pages to remove */
  2183. first_page = list_entry(rb_list_head(to_remove->next),
  2184. struct buffer_page, list);
  2185. for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
  2186. to_remove = rb_list_head(to_remove)->next;
  2187. head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
  2188. }
  2189. /* Read iterators need to reset themselves when some pages removed */
  2190. cpu_buffer->pages_removed += nr_removed;
  2191. next_page = rb_list_head(to_remove)->next;
  2192. /*
  2193. * Now we remove all pages between tail_page and next_page.
  2194. * Make sure that we have head_bit value preserved for the
  2195. * next page
  2196. */
  2197. tail_page->next = (struct list_head *)((unsigned long)next_page |
  2198. head_bit);
  2199. next_page = rb_list_head(next_page);
  2200. next_page->prev = tail_page;
  2201. /* make sure pages points to a valid page in the ring buffer */
  2202. cpu_buffer->pages = next_page;
  2203. cpu_buffer->cnt++;
  2204. /* update head page */
  2205. if (head_bit)
  2206. cpu_buffer->head_page = list_entry(next_page,
  2207. struct buffer_page, list);
  2208. /* pages are removed, resume tracing and then free the pages */
  2209. atomic_dec(&cpu_buffer->record_disabled);
  2210. raw_spin_unlock_irq(&cpu_buffer->reader_lock);
  2211. RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
  2212. /* last buffer page to remove */
  2213. last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
  2214. list);
  2215. tmp_iter_page = first_page;
  2216. do {
  2217. cond_resched();
  2218. to_remove_page = tmp_iter_page;
  2219. rb_inc_page(&tmp_iter_page);
  2220. /* update the counters */
  2221. page_entries = rb_page_entries(to_remove_page);
  2222. if (page_entries) {
  2223. /*
  2224. * If something was added to this page, it was full
  2225. * since it is not the tail page. So we deduct the
  2226. * bytes consumed in ring buffer from here.
  2227. * Increment overrun to account for the lost events.
  2228. */
  2229. local_add(page_entries, &cpu_buffer->overrun);
  2230. local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes);
  2231. local_inc(&cpu_buffer->pages_lost);
  2232. }
  2233. /*
  2234. * We have already removed references to this list item, just
  2235. * free up the buffer_page and its page
  2236. */
  2237. free_buffer_page(to_remove_page);
  2238. nr_removed--;
  2239. } while (to_remove_page != last_page);
  2240. RB_WARN_ON(cpu_buffer, nr_removed);
  2241. return nr_removed == 0;
  2242. }
  2243. static bool
  2244. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
  2245. {
  2246. struct list_head *pages = &cpu_buffer->new_pages;
  2247. unsigned long flags;
  2248. bool success;
  2249. int retries;
  2250. /* Can be called at early boot up, where interrupts must not been enabled */
  2251. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2252. /*
  2253. * We are holding the reader lock, so the reader page won't be swapped
  2254. * in the ring buffer. Now we are racing with the writer trying to
  2255. * move head page and the tail page.
  2256. * We are going to adapt the reader page update process where:
  2257. * 1. We first splice the start and end of list of new pages between
  2258. * the head page and its previous page.
  2259. * 2. We cmpxchg the prev_page->next to point from head page to the
  2260. * start of new pages list.
  2261. * 3. Finally, we update the head->prev to the end of new list.
  2262. *
  2263. * We will try this process 10 times, to make sure that we don't keep
  2264. * spinning.
  2265. */
  2266. retries = 10;
  2267. success = false;
  2268. while (retries--) {
  2269. struct list_head *head_page, *prev_page;
  2270. struct list_head *last_page, *first_page;
  2271. struct list_head *head_page_with_bit;
  2272. struct buffer_page *hpage = rb_set_head_page(cpu_buffer);
  2273. if (!hpage)
  2274. break;
  2275. head_page = &hpage->list;
  2276. prev_page = head_page->prev;
  2277. first_page = pages->next;
  2278. last_page = pages->prev;
  2279. head_page_with_bit = (struct list_head *)
  2280. ((unsigned long)head_page | RB_PAGE_HEAD);
  2281. last_page->next = head_page_with_bit;
  2282. first_page->prev = prev_page;
  2283. /* caution: head_page_with_bit gets updated on cmpxchg failure */
  2284. if (try_cmpxchg(&prev_page->next,
  2285. &head_page_with_bit, first_page)) {
  2286. /*
  2287. * yay, we replaced the page pointer to our new list,
  2288. * now, we just have to update to head page's prev
  2289. * pointer to point to end of list
  2290. */
  2291. head_page->prev = last_page;
  2292. cpu_buffer->cnt++;
  2293. success = true;
  2294. break;
  2295. }
  2296. }
  2297. if (success)
  2298. INIT_LIST_HEAD(pages);
  2299. /*
  2300. * If we weren't successful in adding in new pages, warn and stop
  2301. * tracing
  2302. */
  2303. RB_WARN_ON(cpu_buffer, !success);
  2304. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2305. /* free pages if they weren't inserted */
  2306. if (!success) {
  2307. struct buffer_page *bpage, *tmp;
  2308. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  2309. list) {
  2310. list_del_init(&bpage->list);
  2311. free_buffer_page(bpage);
  2312. }
  2313. }
  2314. return success;
  2315. }
  2316. static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
  2317. {
  2318. bool success;
  2319. if (cpu_buffer->nr_pages_to_update > 0)
  2320. success = rb_insert_pages(cpu_buffer);
  2321. else
  2322. success = rb_remove_pages(cpu_buffer,
  2323. -cpu_buffer->nr_pages_to_update);
  2324. if (success)
  2325. cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
  2326. }
  2327. static void update_pages_handler(struct work_struct *work)
  2328. {
  2329. struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
  2330. struct ring_buffer_per_cpu, update_pages_work);
  2331. rb_update_pages(cpu_buffer);
  2332. complete(&cpu_buffer->update_done);
  2333. }
  2334. /**
  2335. * ring_buffer_resize - resize the ring buffer
  2336. * @buffer: the buffer to resize.
  2337. * @size: the new size.
  2338. * @cpu_id: the cpu buffer to resize
  2339. *
  2340. * Minimum size is 2 * buffer->subbuf_size.
  2341. *
  2342. * Returns 0 on success and < 0 on failure.
  2343. */
  2344. int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
  2345. int cpu_id)
  2346. {
  2347. struct ring_buffer_per_cpu *cpu_buffer;
  2348. unsigned long nr_pages;
  2349. int cpu, err;
  2350. /*
  2351. * Always succeed at resizing a non-existent buffer:
  2352. */
  2353. if (!buffer)
  2354. return 0;
  2355. /* Make sure the requested buffer exists */
  2356. if (cpu_id != RING_BUFFER_ALL_CPUS &&
  2357. !cpumask_test_cpu(cpu_id, buffer->cpumask))
  2358. return 0;
  2359. nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size);
  2360. /* we need a minimum of two pages */
  2361. if (nr_pages < 2)
  2362. nr_pages = 2;
  2363. /* prevent another thread from changing buffer sizes */
  2364. mutex_lock(&buffer->mutex);
  2365. atomic_inc(&buffer->resizing);
  2366. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  2367. /*
  2368. * Don't succeed if resizing is disabled, as a reader might be
  2369. * manipulating the ring buffer and is expecting a sane state while
  2370. * this is true.
  2371. */
  2372. for_each_buffer_cpu(buffer, cpu) {
  2373. cpu_buffer = buffer->buffers[cpu];
  2374. if (atomic_read(&cpu_buffer->resize_disabled)) {
  2375. err = -EBUSY;
  2376. goto out_err_unlock;
  2377. }
  2378. }
  2379. /* calculate the pages to update */
  2380. for_each_buffer_cpu(buffer, cpu) {
  2381. cpu_buffer = buffer->buffers[cpu];
  2382. cpu_buffer->nr_pages_to_update = nr_pages -
  2383. cpu_buffer->nr_pages;
  2384. /*
  2385. * nothing more to do for removing pages or no update
  2386. */
  2387. if (cpu_buffer->nr_pages_to_update <= 0)
  2388. continue;
  2389. /*
  2390. * to add pages, make sure all new pages can be
  2391. * allocated without receiving ENOMEM
  2392. */
  2393. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  2394. if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
  2395. &cpu_buffer->new_pages)) {
  2396. /* not enough memory for new pages */
  2397. err = -ENOMEM;
  2398. goto out_err;
  2399. }
  2400. cond_resched();
  2401. }
  2402. cpus_read_lock();
  2403. /*
  2404. * Fire off all the required work handlers
  2405. * We can't schedule on offline CPUs, but it's not necessary
  2406. * since we can change their buffer sizes without any race.
  2407. */
  2408. for_each_buffer_cpu(buffer, cpu) {
  2409. cpu_buffer = buffer->buffers[cpu];
  2410. if (!cpu_buffer->nr_pages_to_update)
  2411. continue;
  2412. /* Can't run something on an offline CPU. */
  2413. if (!cpu_online(cpu)) {
  2414. rb_update_pages(cpu_buffer);
  2415. cpu_buffer->nr_pages_to_update = 0;
  2416. } else {
  2417. /* Run directly if possible. */
  2418. migrate_disable();
  2419. if (cpu != smp_processor_id()) {
  2420. migrate_enable();
  2421. schedule_work_on(cpu,
  2422. &cpu_buffer->update_pages_work);
  2423. } else {
  2424. update_pages_handler(&cpu_buffer->update_pages_work);
  2425. migrate_enable();
  2426. }
  2427. }
  2428. }
  2429. /* wait for all the updates to complete */
  2430. for_each_buffer_cpu(buffer, cpu) {
  2431. cpu_buffer = buffer->buffers[cpu];
  2432. if (!cpu_buffer->nr_pages_to_update)
  2433. continue;
  2434. if (cpu_online(cpu))
  2435. wait_for_completion(&cpu_buffer->update_done);
  2436. cpu_buffer->nr_pages_to_update = 0;
  2437. }
  2438. cpus_read_unlock();
  2439. } else {
  2440. cpu_buffer = buffer->buffers[cpu_id];
  2441. if (nr_pages == cpu_buffer->nr_pages)
  2442. goto out;
  2443. /*
  2444. * Don't succeed if resizing is disabled, as a reader might be
  2445. * manipulating the ring buffer and is expecting a sane state while
  2446. * this is true.
  2447. */
  2448. if (atomic_read(&cpu_buffer->resize_disabled)) {
  2449. err = -EBUSY;
  2450. goto out_err_unlock;
  2451. }
  2452. cpu_buffer->nr_pages_to_update = nr_pages -
  2453. cpu_buffer->nr_pages;
  2454. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  2455. if (cpu_buffer->nr_pages_to_update > 0 &&
  2456. __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
  2457. &cpu_buffer->new_pages)) {
  2458. err = -ENOMEM;
  2459. goto out_err;
  2460. }
  2461. cpus_read_lock();
  2462. /* Can't run something on an offline CPU. */
  2463. if (!cpu_online(cpu_id))
  2464. rb_update_pages(cpu_buffer);
  2465. else {
  2466. /* Run directly if possible. */
  2467. migrate_disable();
  2468. if (cpu_id == smp_processor_id()) {
  2469. rb_update_pages(cpu_buffer);
  2470. migrate_enable();
  2471. } else {
  2472. migrate_enable();
  2473. schedule_work_on(cpu_id,
  2474. &cpu_buffer->update_pages_work);
  2475. wait_for_completion(&cpu_buffer->update_done);
  2476. }
  2477. }
  2478. cpu_buffer->nr_pages_to_update = 0;
  2479. cpus_read_unlock();
  2480. }
  2481. out:
  2482. /*
  2483. * The ring buffer resize can happen with the ring buffer
  2484. * enabled, so that the update disturbs the tracing as little
  2485. * as possible. But if the buffer is disabled, we do not need
  2486. * to worry about that, and we can take the time to verify
  2487. * that the buffer is not corrupt.
  2488. */
  2489. if (atomic_read(&buffer->record_disabled)) {
  2490. atomic_inc(&buffer->record_disabled);
  2491. /*
  2492. * Even though the buffer was disabled, we must make sure
  2493. * that it is truly disabled before calling rb_check_pages.
  2494. * There could have been a race between checking
  2495. * record_disable and incrementing it.
  2496. */
  2497. synchronize_rcu();
  2498. for_each_buffer_cpu(buffer, cpu) {
  2499. cpu_buffer = buffer->buffers[cpu];
  2500. rb_check_pages(cpu_buffer);
  2501. }
  2502. atomic_dec(&buffer->record_disabled);
  2503. }
  2504. atomic_dec(&buffer->resizing);
  2505. mutex_unlock(&buffer->mutex);
  2506. return 0;
  2507. out_err:
  2508. for_each_buffer_cpu(buffer, cpu) {
  2509. struct buffer_page *bpage, *tmp;
  2510. cpu_buffer = buffer->buffers[cpu];
  2511. cpu_buffer->nr_pages_to_update = 0;
  2512. if (list_empty(&cpu_buffer->new_pages))
  2513. continue;
  2514. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
  2515. list) {
  2516. list_del_init(&bpage->list);
  2517. free_buffer_page(bpage);
  2518. }
  2519. }
  2520. out_err_unlock:
  2521. atomic_dec(&buffer->resizing);
  2522. mutex_unlock(&buffer->mutex);
  2523. return err;
  2524. }
  2525. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  2526. void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
  2527. {
  2528. mutex_lock(&buffer->mutex);
  2529. if (val)
  2530. buffer->flags |= RB_FL_OVERWRITE;
  2531. else
  2532. buffer->flags &= ~RB_FL_OVERWRITE;
  2533. mutex_unlock(&buffer->mutex);
  2534. }
  2535. EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
  2536. static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  2537. {
  2538. return bpage->page->data + index;
  2539. }
  2540. static __always_inline struct ring_buffer_event *
  2541. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  2542. {
  2543. return __rb_page_index(cpu_buffer->reader_page,
  2544. cpu_buffer->reader_page->read);
  2545. }
  2546. static struct ring_buffer_event *
  2547. rb_iter_head_event(struct ring_buffer_iter *iter)
  2548. {
  2549. struct ring_buffer_event *event;
  2550. struct buffer_page *iter_head_page = iter->head_page;
  2551. unsigned long commit;
  2552. unsigned length;
  2553. if (iter->head != iter->next_event)
  2554. return iter->event;
  2555. /*
  2556. * When the writer goes across pages, it issues a cmpxchg which
  2557. * is a mb(), which will synchronize with the rmb here.
  2558. * (see rb_tail_page_update() and __rb_reserve_next())
  2559. */
  2560. commit = rb_page_commit(iter_head_page);
  2561. smp_rmb();
  2562. /* An event needs to be at least 8 bytes in size */
  2563. if (iter->head > commit - 8)
  2564. goto reset;
  2565. event = __rb_page_index(iter_head_page, iter->head);
  2566. length = rb_event_length(event);
  2567. /*
  2568. * READ_ONCE() doesn't work on functions and we don't want the
  2569. * compiler doing any crazy optimizations with length.
  2570. */
  2571. barrier();
  2572. if ((iter->head + length) > commit || length > iter->event_size)
  2573. /* Writer corrupted the read? */
  2574. goto reset;
  2575. memcpy(iter->event, event, length);
  2576. /*
  2577. * If the page stamp is still the same after this rmb() then the
  2578. * event was safely copied without the writer entering the page.
  2579. */
  2580. smp_rmb();
  2581. /* Make sure the page didn't change since we read this */
  2582. if (iter->page_stamp != iter_head_page->page->time_stamp ||
  2583. commit > rb_page_commit(iter_head_page))
  2584. goto reset;
  2585. iter->next_event = iter->head + length;
  2586. return iter->event;
  2587. reset:
  2588. /* Reset to the beginning */
  2589. iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
  2590. iter->head = 0;
  2591. iter->next_event = 0;
  2592. iter->missed_events = 1;
  2593. return NULL;
  2594. }
  2595. /* Size is determined by what has been committed */
  2596. static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
  2597. {
  2598. return rb_page_commit(bpage) & ~RB_MISSED_MASK;
  2599. }
  2600. static __always_inline unsigned
  2601. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  2602. {
  2603. return rb_page_commit(cpu_buffer->commit_page);
  2604. }
  2605. static __always_inline unsigned
  2606. rb_event_index(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event *event)
  2607. {
  2608. unsigned long addr = (unsigned long)event;
  2609. addr &= (PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1;
  2610. return addr - BUF_PAGE_HDR_SIZE;
  2611. }
  2612. static void rb_inc_iter(struct ring_buffer_iter *iter)
  2613. {
  2614. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2615. /*
  2616. * The iterator could be on the reader page (it starts there).
  2617. * But the head could have moved, since the reader was
  2618. * found. Check for this case and assign the iterator
  2619. * to the head page instead of next.
  2620. */
  2621. if (iter->head_page == cpu_buffer->reader_page)
  2622. iter->head_page = rb_set_head_page(cpu_buffer);
  2623. else
  2624. rb_inc_page(&iter->head_page);
  2625. iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
  2626. iter->head = 0;
  2627. iter->next_event = 0;
  2628. }
  2629. /* Return the index into the sub-buffers for a given sub-buffer */
  2630. static int rb_meta_subbuf_idx(struct ring_buffer_meta *meta, void *subbuf)
  2631. {
  2632. void *subbuf_array;
  2633. subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs;
  2634. subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size);
  2635. return (subbuf - subbuf_array) / meta->subbuf_size;
  2636. }
  2637. static void rb_update_meta_head(struct ring_buffer_per_cpu *cpu_buffer,
  2638. struct buffer_page *next_page)
  2639. {
  2640. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  2641. unsigned long old_head = (unsigned long)next_page->page;
  2642. unsigned long new_head;
  2643. rb_inc_page(&next_page);
  2644. new_head = (unsigned long)next_page->page;
  2645. /*
  2646. * Only move it forward once, if something else came in and
  2647. * moved it forward, then we don't want to touch it.
  2648. */
  2649. (void)cmpxchg(&meta->head_buffer, old_head, new_head);
  2650. }
  2651. static void rb_update_meta_reader(struct ring_buffer_per_cpu *cpu_buffer,
  2652. struct buffer_page *reader)
  2653. {
  2654. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  2655. void *old_reader = cpu_buffer->reader_page->page;
  2656. void *new_reader = reader->page;
  2657. int id;
  2658. id = reader->id;
  2659. cpu_buffer->reader_page->id = id;
  2660. reader->id = 0;
  2661. meta->buffers[0] = rb_meta_subbuf_idx(meta, new_reader);
  2662. meta->buffers[id] = rb_meta_subbuf_idx(meta, old_reader);
  2663. /* The head pointer is the one after the reader */
  2664. rb_update_meta_head(cpu_buffer, reader);
  2665. }
  2666. /*
  2667. * rb_handle_head_page - writer hit the head page
  2668. *
  2669. * Returns: +1 to retry page
  2670. * 0 to continue
  2671. * -1 on error
  2672. */
  2673. static int
  2674. rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  2675. struct buffer_page *tail_page,
  2676. struct buffer_page *next_page)
  2677. {
  2678. struct buffer_page *new_head;
  2679. int entries;
  2680. int type;
  2681. int ret;
  2682. entries = rb_page_entries(next_page);
  2683. /*
  2684. * The hard part is here. We need to move the head
  2685. * forward, and protect against both readers on
  2686. * other CPUs and writers coming in via interrupts.
  2687. */
  2688. type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
  2689. RB_PAGE_HEAD);
  2690. /*
  2691. * type can be one of four:
  2692. * NORMAL - an interrupt already moved it for us
  2693. * HEAD - we are the first to get here.
  2694. * UPDATE - we are the interrupt interrupting
  2695. * a current move.
  2696. * MOVED - a reader on another CPU moved the next
  2697. * pointer to its reader page. Give up
  2698. * and try again.
  2699. */
  2700. switch (type) {
  2701. case RB_PAGE_HEAD:
  2702. /*
  2703. * We changed the head to UPDATE, thus
  2704. * it is our responsibility to update
  2705. * the counters.
  2706. */
  2707. local_add(entries, &cpu_buffer->overrun);
  2708. local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes);
  2709. local_inc(&cpu_buffer->pages_lost);
  2710. if (cpu_buffer->ring_meta)
  2711. rb_update_meta_head(cpu_buffer, next_page);
  2712. /*
  2713. * The entries will be zeroed out when we move the
  2714. * tail page.
  2715. */
  2716. /* still more to do */
  2717. break;
  2718. case RB_PAGE_UPDATE:
  2719. /*
  2720. * This is an interrupt that interrupt the
  2721. * previous update. Still more to do.
  2722. */
  2723. break;
  2724. case RB_PAGE_NORMAL:
  2725. /*
  2726. * An interrupt came in before the update
  2727. * and processed this for us.
  2728. * Nothing left to do.
  2729. */
  2730. return 1;
  2731. case RB_PAGE_MOVED:
  2732. /*
  2733. * The reader is on another CPU and just did
  2734. * a swap with our next_page.
  2735. * Try again.
  2736. */
  2737. return 1;
  2738. default:
  2739. RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
  2740. return -1;
  2741. }
  2742. /*
  2743. * Now that we are here, the old head pointer is
  2744. * set to UPDATE. This will keep the reader from
  2745. * swapping the head page with the reader page.
  2746. * The reader (on another CPU) will spin till
  2747. * we are finished.
  2748. *
  2749. * We just need to protect against interrupts
  2750. * doing the job. We will set the next pointer
  2751. * to HEAD. After that, we set the old pointer
  2752. * to NORMAL, but only if it was HEAD before.
  2753. * otherwise we are an interrupt, and only
  2754. * want the outer most commit to reset it.
  2755. */
  2756. new_head = next_page;
  2757. rb_inc_page(&new_head);
  2758. ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
  2759. RB_PAGE_NORMAL);
  2760. /*
  2761. * Valid returns are:
  2762. * HEAD - an interrupt came in and already set it.
  2763. * NORMAL - One of two things:
  2764. * 1) We really set it.
  2765. * 2) A bunch of interrupts came in and moved
  2766. * the page forward again.
  2767. */
  2768. switch (ret) {
  2769. case RB_PAGE_HEAD:
  2770. case RB_PAGE_NORMAL:
  2771. /* OK */
  2772. break;
  2773. default:
  2774. RB_WARN_ON(cpu_buffer, 1);
  2775. return -1;
  2776. }
  2777. /*
  2778. * It is possible that an interrupt came in,
  2779. * set the head up, then more interrupts came in
  2780. * and moved it again. When we get back here,
  2781. * the page would have been set to NORMAL but we
  2782. * just set it back to HEAD.
  2783. *
  2784. * How do you detect this? Well, if that happened
  2785. * the tail page would have moved.
  2786. */
  2787. if (ret == RB_PAGE_NORMAL) {
  2788. struct buffer_page *buffer_tail_page;
  2789. buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
  2790. /*
  2791. * If the tail had moved passed next, then we need
  2792. * to reset the pointer.
  2793. */
  2794. if (buffer_tail_page != tail_page &&
  2795. buffer_tail_page != next_page)
  2796. rb_head_page_set_normal(cpu_buffer, new_head,
  2797. next_page,
  2798. RB_PAGE_HEAD);
  2799. }
  2800. /*
  2801. * If this was the outer most commit (the one that
  2802. * changed the original pointer from HEAD to UPDATE),
  2803. * then it is up to us to reset it to NORMAL.
  2804. */
  2805. if (type == RB_PAGE_HEAD) {
  2806. ret = rb_head_page_set_normal(cpu_buffer, next_page,
  2807. tail_page,
  2808. RB_PAGE_UPDATE);
  2809. if (RB_WARN_ON(cpu_buffer,
  2810. ret != RB_PAGE_UPDATE))
  2811. return -1;
  2812. }
  2813. return 0;
  2814. }
  2815. static inline void
  2816. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  2817. unsigned long tail, struct rb_event_info *info)
  2818. {
  2819. unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
  2820. struct buffer_page *tail_page = info->tail_page;
  2821. struct ring_buffer_event *event;
  2822. unsigned long length = info->length;
  2823. /*
  2824. * Only the event that crossed the page boundary
  2825. * must fill the old tail_page with padding.
  2826. */
  2827. if (tail >= bsize) {
  2828. /*
  2829. * If the page was filled, then we still need
  2830. * to update the real_end. Reset it to zero
  2831. * and the reader will ignore it.
  2832. */
  2833. if (tail == bsize)
  2834. tail_page->real_end = 0;
  2835. local_sub(length, &tail_page->write);
  2836. return;
  2837. }
  2838. event = __rb_page_index(tail_page, tail);
  2839. /*
  2840. * Save the original length to the meta data.
  2841. * This will be used by the reader to add lost event
  2842. * counter.
  2843. */
  2844. tail_page->real_end = tail;
  2845. /*
  2846. * If this event is bigger than the minimum size, then
  2847. * we need to be careful that we don't subtract the
  2848. * write counter enough to allow another writer to slip
  2849. * in on this page.
  2850. * We put in a discarded commit instead, to make sure
  2851. * that this space is not used again, and this space will
  2852. * not be accounted into 'entries_bytes'.
  2853. *
  2854. * If we are less than the minimum size, we don't need to
  2855. * worry about it.
  2856. */
  2857. if (tail > (bsize - RB_EVNT_MIN_SIZE)) {
  2858. /* No room for any events */
  2859. /* Mark the rest of the page with padding */
  2860. rb_event_set_padding(event);
  2861. /* Make sure the padding is visible before the write update */
  2862. smp_wmb();
  2863. /* Set the write back to the previous setting */
  2864. local_sub(length, &tail_page->write);
  2865. return;
  2866. }
  2867. /* Put in a discarded event */
  2868. event->array[0] = (bsize - tail) - RB_EVNT_HDR_SIZE;
  2869. event->type_len = RINGBUF_TYPE_PADDING;
  2870. /* time delta must be non zero */
  2871. event->time_delta = 1;
  2872. /* account for padding bytes */
  2873. local_add(bsize - tail, &cpu_buffer->entries_bytes);
  2874. /* Make sure the padding is visible before the tail_page->write update */
  2875. smp_wmb();
  2876. /* Set write to end of buffer */
  2877. length = (tail + length) - bsize;
  2878. local_sub(length, &tail_page->write);
  2879. }
  2880. static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
  2881. /*
  2882. * This is the slow path, force gcc not to inline it.
  2883. */
  2884. static noinline struct ring_buffer_event *
  2885. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  2886. unsigned long tail, struct rb_event_info *info)
  2887. {
  2888. struct buffer_page *tail_page = info->tail_page;
  2889. struct buffer_page *commit_page = cpu_buffer->commit_page;
  2890. struct trace_buffer *buffer = cpu_buffer->buffer;
  2891. struct buffer_page *next_page;
  2892. int ret;
  2893. next_page = tail_page;
  2894. rb_inc_page(&next_page);
  2895. /*
  2896. * If for some reason, we had an interrupt storm that made
  2897. * it all the way around the buffer, bail, and warn
  2898. * about it.
  2899. */
  2900. if (unlikely(next_page == commit_page)) {
  2901. local_inc(&cpu_buffer->commit_overrun);
  2902. goto out_reset;
  2903. }
  2904. /*
  2905. * This is where the fun begins!
  2906. *
  2907. * We are fighting against races between a reader that
  2908. * could be on another CPU trying to swap its reader
  2909. * page with the buffer head.
  2910. *
  2911. * We are also fighting against interrupts coming in and
  2912. * moving the head or tail on us as well.
  2913. *
  2914. * If the next page is the head page then we have filled
  2915. * the buffer, unless the commit page is still on the
  2916. * reader page.
  2917. */
  2918. if (rb_is_head_page(next_page, &tail_page->list)) {
  2919. /*
  2920. * If the commit is not on the reader page, then
  2921. * move the header page.
  2922. */
  2923. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  2924. /*
  2925. * If we are not in overwrite mode,
  2926. * this is easy, just stop here.
  2927. */
  2928. if (!(buffer->flags & RB_FL_OVERWRITE)) {
  2929. local_inc(&cpu_buffer->dropped_events);
  2930. goto out_reset;
  2931. }
  2932. ret = rb_handle_head_page(cpu_buffer,
  2933. tail_page,
  2934. next_page);
  2935. if (ret < 0)
  2936. goto out_reset;
  2937. if (ret)
  2938. goto out_again;
  2939. } else {
  2940. /*
  2941. * We need to be careful here too. The
  2942. * commit page could still be on the reader
  2943. * page. We could have a small buffer, and
  2944. * have filled up the buffer with events
  2945. * from interrupts and such, and wrapped.
  2946. *
  2947. * Note, if the tail page is also on the
  2948. * reader_page, we let it move out.
  2949. */
  2950. if (unlikely((cpu_buffer->commit_page !=
  2951. cpu_buffer->tail_page) &&
  2952. (cpu_buffer->commit_page ==
  2953. cpu_buffer->reader_page))) {
  2954. local_inc(&cpu_buffer->commit_overrun);
  2955. goto out_reset;
  2956. }
  2957. }
  2958. }
  2959. rb_tail_page_update(cpu_buffer, tail_page, next_page);
  2960. out_again:
  2961. rb_reset_tail(cpu_buffer, tail, info);
  2962. /* Commit what we have for now. */
  2963. rb_end_commit(cpu_buffer);
  2964. /* rb_end_commit() decs committing */
  2965. local_inc(&cpu_buffer->committing);
  2966. /* fail and let the caller try again */
  2967. return ERR_PTR(-EAGAIN);
  2968. out_reset:
  2969. /* reset write */
  2970. rb_reset_tail(cpu_buffer, tail, info);
  2971. return NULL;
  2972. }
  2973. /* Slow path */
  2974. static struct ring_buffer_event *
  2975. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2976. struct ring_buffer_event *event, u64 delta, bool abs)
  2977. {
  2978. if (abs)
  2979. event->type_len = RINGBUF_TYPE_TIME_STAMP;
  2980. else
  2981. event->type_len = RINGBUF_TYPE_TIME_EXTEND;
  2982. /* Not the first event on the page, or not delta? */
  2983. if (abs || rb_event_index(cpu_buffer, event)) {
  2984. event->time_delta = delta & TS_MASK;
  2985. event->array[0] = delta >> TS_SHIFT;
  2986. } else {
  2987. /* nope, just zero it */
  2988. event->time_delta = 0;
  2989. event->array[0] = 0;
  2990. }
  2991. return skip_time_extend(event);
  2992. }
  2993. #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  2994. static inline bool sched_clock_stable(void)
  2995. {
  2996. return true;
  2997. }
  2998. #endif
  2999. static void
  3000. rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
  3001. struct rb_event_info *info)
  3002. {
  3003. u64 write_stamp;
  3004. WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
  3005. (unsigned long long)info->delta,
  3006. (unsigned long long)info->ts,
  3007. (unsigned long long)info->before,
  3008. (unsigned long long)info->after,
  3009. (unsigned long long)({rb_time_read(&cpu_buffer->write_stamp, &write_stamp); write_stamp;}),
  3010. sched_clock_stable() ? "" :
  3011. "If you just came from a suspend/resume,\n"
  3012. "please switch to the trace global clock:\n"
  3013. " echo global > /sys/kernel/tracing/trace_clock\n"
  3014. "or add trace_clock=global to the kernel command line\n");
  3015. }
  3016. static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
  3017. struct ring_buffer_event **event,
  3018. struct rb_event_info *info,
  3019. u64 *delta,
  3020. unsigned int *length)
  3021. {
  3022. bool abs = info->add_timestamp &
  3023. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
  3024. if (unlikely(info->delta > (1ULL << 59))) {
  3025. /*
  3026. * Some timers can use more than 59 bits, and when a timestamp
  3027. * is added to the buffer, it will lose those bits.
  3028. */
  3029. if (abs && (info->ts & TS_MSB)) {
  3030. info->delta &= ABS_TS_MASK;
  3031. /* did the clock go backwards */
  3032. } else if (info->before == info->after && info->before > info->ts) {
  3033. /* not interrupted */
  3034. static int once;
  3035. /*
  3036. * This is possible with a recalibrating of the TSC.
  3037. * Do not produce a call stack, but just report it.
  3038. */
  3039. if (!once) {
  3040. once++;
  3041. pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
  3042. info->before, info->ts);
  3043. }
  3044. } else
  3045. rb_check_timestamp(cpu_buffer, info);
  3046. if (!abs)
  3047. info->delta = 0;
  3048. }
  3049. *event = rb_add_time_stamp(cpu_buffer, *event, info->delta, abs);
  3050. *length -= RB_LEN_TIME_EXTEND;
  3051. *delta = 0;
  3052. }
  3053. /**
  3054. * rb_update_event - update event type and data
  3055. * @cpu_buffer: The per cpu buffer of the @event
  3056. * @event: the event to update
  3057. * @info: The info to update the @event with (contains length and delta)
  3058. *
  3059. * Update the type and data fields of the @event. The length
  3060. * is the actual size that is written to the ring buffer,
  3061. * and with this, we can determine what to place into the
  3062. * data field.
  3063. */
  3064. static void
  3065. rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
  3066. struct ring_buffer_event *event,
  3067. struct rb_event_info *info)
  3068. {
  3069. unsigned length = info->length;
  3070. u64 delta = info->delta;
  3071. unsigned int nest = local_read(&cpu_buffer->committing) - 1;
  3072. if (!WARN_ON_ONCE(nest >= MAX_NEST))
  3073. cpu_buffer->event_stamp[nest] = info->ts;
  3074. /*
  3075. * If we need to add a timestamp, then we
  3076. * add it to the start of the reserved space.
  3077. */
  3078. if (unlikely(info->add_timestamp))
  3079. rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
  3080. event->time_delta = delta;
  3081. length -= RB_EVNT_HDR_SIZE;
  3082. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
  3083. event->type_len = 0;
  3084. event->array[0] = length;
  3085. } else
  3086. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  3087. }
  3088. static unsigned rb_calculate_event_length(unsigned length)
  3089. {
  3090. struct ring_buffer_event event; /* Used only for sizeof array */
  3091. /* zero length can cause confusions */
  3092. if (!length)
  3093. length++;
  3094. if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
  3095. length += sizeof(event.array[0]);
  3096. length += RB_EVNT_HDR_SIZE;
  3097. length = ALIGN(length, RB_ARCH_ALIGNMENT);
  3098. /*
  3099. * In case the time delta is larger than the 27 bits for it
  3100. * in the header, we need to add a timestamp. If another
  3101. * event comes in when trying to discard this one to increase
  3102. * the length, then the timestamp will be added in the allocated
  3103. * space of this event. If length is bigger than the size needed
  3104. * for the TIME_EXTEND, then padding has to be used. The events
  3105. * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
  3106. * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
  3107. * As length is a multiple of 4, we only need to worry if it
  3108. * is 12 (RB_LEN_TIME_EXTEND + 4).
  3109. */
  3110. if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
  3111. length += RB_ALIGNMENT;
  3112. return length;
  3113. }
  3114. static inline bool
  3115. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  3116. struct ring_buffer_event *event)
  3117. {
  3118. unsigned long new_index, old_index;
  3119. struct buffer_page *bpage;
  3120. unsigned long addr;
  3121. new_index = rb_event_index(cpu_buffer, event);
  3122. old_index = new_index + rb_event_ts_length(event);
  3123. addr = (unsigned long)event;
  3124. addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
  3125. bpage = READ_ONCE(cpu_buffer->tail_page);
  3126. /*
  3127. * Make sure the tail_page is still the same and
  3128. * the next write location is the end of this event
  3129. */
  3130. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  3131. unsigned long write_mask =
  3132. local_read(&bpage->write) & ~RB_WRITE_MASK;
  3133. unsigned long event_length = rb_event_length(event);
  3134. /*
  3135. * For the before_stamp to be different than the write_stamp
  3136. * to make sure that the next event adds an absolute
  3137. * value and does not rely on the saved write stamp, which
  3138. * is now going to be bogus.
  3139. *
  3140. * By setting the before_stamp to zero, the next event
  3141. * is not going to use the write_stamp and will instead
  3142. * create an absolute timestamp. This means there's no
  3143. * reason to update the wirte_stamp!
  3144. */
  3145. rb_time_set(&cpu_buffer->before_stamp, 0);
  3146. /*
  3147. * If an event were to come in now, it would see that the
  3148. * write_stamp and the before_stamp are different, and assume
  3149. * that this event just added itself before updating
  3150. * the write stamp. The interrupting event will fix the
  3151. * write stamp for us, and use an absolute timestamp.
  3152. */
  3153. /*
  3154. * This is on the tail page. It is possible that
  3155. * a write could come in and move the tail page
  3156. * and write to the next page. That is fine
  3157. * because we just shorten what is on this page.
  3158. */
  3159. old_index += write_mask;
  3160. new_index += write_mask;
  3161. /* caution: old_index gets updated on cmpxchg failure */
  3162. if (local_try_cmpxchg(&bpage->write, &old_index, new_index)) {
  3163. /* update counters */
  3164. local_sub(event_length, &cpu_buffer->entries_bytes);
  3165. return true;
  3166. }
  3167. }
  3168. /* could not discard */
  3169. return false;
  3170. }
  3171. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3172. {
  3173. local_inc(&cpu_buffer->committing);
  3174. local_inc(&cpu_buffer->commits);
  3175. }
  3176. static __always_inline void
  3177. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  3178. {
  3179. unsigned long max_count;
  3180. /*
  3181. * We only race with interrupts and NMIs on this CPU.
  3182. * If we own the commit event, then we can commit
  3183. * all others that interrupted us, since the interruptions
  3184. * are in stack format (they finish before they come
  3185. * back to us). This allows us to do a simple loop to
  3186. * assign the commit to the tail.
  3187. */
  3188. again:
  3189. max_count = cpu_buffer->nr_pages * 100;
  3190. while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
  3191. if (RB_WARN_ON(cpu_buffer, !(--max_count)))
  3192. return;
  3193. if (RB_WARN_ON(cpu_buffer,
  3194. rb_is_reader_page(cpu_buffer->tail_page)))
  3195. return;
  3196. /*
  3197. * No need for a memory barrier here, as the update
  3198. * of the tail_page did it for this page.
  3199. */
  3200. local_set(&cpu_buffer->commit_page->page->commit,
  3201. rb_page_write(cpu_buffer->commit_page));
  3202. rb_inc_page(&cpu_buffer->commit_page);
  3203. if (cpu_buffer->ring_meta) {
  3204. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  3205. meta->commit_buffer = (unsigned long)cpu_buffer->commit_page->page;
  3206. }
  3207. /* add barrier to keep gcc from optimizing too much */
  3208. barrier();
  3209. }
  3210. while (rb_commit_index(cpu_buffer) !=
  3211. rb_page_write(cpu_buffer->commit_page)) {
  3212. /* Make sure the readers see the content of what is committed. */
  3213. smp_wmb();
  3214. local_set(&cpu_buffer->commit_page->page->commit,
  3215. rb_page_write(cpu_buffer->commit_page));
  3216. RB_WARN_ON(cpu_buffer,
  3217. local_read(&cpu_buffer->commit_page->page->commit) &
  3218. ~RB_WRITE_MASK);
  3219. barrier();
  3220. }
  3221. /* again, keep gcc from optimizing */
  3222. barrier();
  3223. /*
  3224. * If an interrupt came in just after the first while loop
  3225. * and pushed the tail page forward, we will be left with
  3226. * a dangling commit that will never go forward.
  3227. */
  3228. if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
  3229. goto again;
  3230. }
  3231. static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3232. {
  3233. unsigned long commits;
  3234. if (RB_WARN_ON(cpu_buffer,
  3235. !local_read(&cpu_buffer->committing)))
  3236. return;
  3237. again:
  3238. commits = local_read(&cpu_buffer->commits);
  3239. /* synchronize with interrupts */
  3240. barrier();
  3241. if (local_read(&cpu_buffer->committing) == 1)
  3242. rb_set_commit_to_write(cpu_buffer);
  3243. local_dec(&cpu_buffer->committing);
  3244. /* synchronize with interrupts */
  3245. barrier();
  3246. /*
  3247. * Need to account for interrupts coming in between the
  3248. * updating of the commit page and the clearing of the
  3249. * committing counter.
  3250. */
  3251. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  3252. !local_read(&cpu_buffer->committing)) {
  3253. local_inc(&cpu_buffer->committing);
  3254. goto again;
  3255. }
  3256. }
  3257. static inline void rb_event_discard(struct ring_buffer_event *event)
  3258. {
  3259. if (extended_time(event))
  3260. event = skip_time_extend(event);
  3261. /* array[0] holds the actual length for the discarded event */
  3262. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  3263. event->type_len = RINGBUF_TYPE_PADDING;
  3264. /* time delta must be non zero */
  3265. if (!event->time_delta)
  3266. event->time_delta = 1;
  3267. }
  3268. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer)
  3269. {
  3270. local_inc(&cpu_buffer->entries);
  3271. rb_end_commit(cpu_buffer);
  3272. }
  3273. static __always_inline void
  3274. rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
  3275. {
  3276. if (buffer->irq_work.waiters_pending) {
  3277. buffer->irq_work.waiters_pending = false;
  3278. /* irq_work_queue() supplies it's own memory barriers */
  3279. irq_work_queue(&buffer->irq_work.work);
  3280. }
  3281. if (cpu_buffer->irq_work.waiters_pending) {
  3282. cpu_buffer->irq_work.waiters_pending = false;
  3283. /* irq_work_queue() supplies it's own memory barriers */
  3284. irq_work_queue(&cpu_buffer->irq_work.work);
  3285. }
  3286. if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
  3287. return;
  3288. if (cpu_buffer->reader_page == cpu_buffer->commit_page)
  3289. return;
  3290. if (!cpu_buffer->irq_work.full_waiters_pending)
  3291. return;
  3292. cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
  3293. if (!full_hit(buffer, cpu_buffer->cpu, cpu_buffer->shortest_full))
  3294. return;
  3295. cpu_buffer->irq_work.wakeup_full = true;
  3296. cpu_buffer->irq_work.full_waiters_pending = false;
  3297. /* irq_work_queue() supplies it's own memory barriers */
  3298. irq_work_queue(&cpu_buffer->irq_work.work);
  3299. }
  3300. #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
  3301. # define do_ring_buffer_record_recursion() \
  3302. do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
  3303. #else
  3304. # define do_ring_buffer_record_recursion() do { } while (0)
  3305. #endif
  3306. /*
  3307. * The lock and unlock are done within a preempt disable section.
  3308. * The current_context per_cpu variable can only be modified
  3309. * by the current task between lock and unlock. But it can
  3310. * be modified more than once via an interrupt. To pass this
  3311. * information from the lock to the unlock without having to
  3312. * access the 'in_interrupt()' functions again (which do show
  3313. * a bit of overhead in something as critical as function tracing,
  3314. * we use a bitmask trick.
  3315. *
  3316. * bit 1 = NMI context
  3317. * bit 2 = IRQ context
  3318. * bit 3 = SoftIRQ context
  3319. * bit 4 = normal context.
  3320. *
  3321. * This works because this is the order of contexts that can
  3322. * preempt other contexts. A SoftIRQ never preempts an IRQ
  3323. * context.
  3324. *
  3325. * When the context is determined, the corresponding bit is
  3326. * checked and set (if it was set, then a recursion of that context
  3327. * happened).
  3328. *
  3329. * On unlock, we need to clear this bit. To do so, just subtract
  3330. * 1 from the current_context and AND it to itself.
  3331. *
  3332. * (binary)
  3333. * 101 - 1 = 100
  3334. * 101 & 100 = 100 (clearing bit zero)
  3335. *
  3336. * 1010 - 1 = 1001
  3337. * 1010 & 1001 = 1000 (clearing bit 1)
  3338. *
  3339. * The least significant bit can be cleared this way, and it
  3340. * just so happens that it is the same bit corresponding to
  3341. * the current context.
  3342. *
  3343. * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
  3344. * is set when a recursion is detected at the current context, and if
  3345. * the TRANSITION bit is already set, it will fail the recursion.
  3346. * This is needed because there's a lag between the changing of
  3347. * interrupt context and updating the preempt count. In this case,
  3348. * a false positive will be found. To handle this, one extra recursion
  3349. * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
  3350. * bit is already set, then it is considered a recursion and the function
  3351. * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
  3352. *
  3353. * On the trace_recursive_unlock(), the TRANSITION bit will be the first
  3354. * to be cleared. Even if it wasn't the context that set it. That is,
  3355. * if an interrupt comes in while NORMAL bit is set and the ring buffer
  3356. * is called before preempt_count() is updated, since the check will
  3357. * be on the NORMAL bit, the TRANSITION bit will then be set. If an
  3358. * NMI then comes in, it will set the NMI bit, but when the NMI code
  3359. * does the trace_recursive_unlock() it will clear the TRANSITION bit
  3360. * and leave the NMI bit set. But this is fine, because the interrupt
  3361. * code that set the TRANSITION bit will then clear the NMI bit when it
  3362. * calls trace_recursive_unlock(). If another NMI comes in, it will
  3363. * set the TRANSITION bit and continue.
  3364. *
  3365. * Note: The TRANSITION bit only handles a single transition between context.
  3366. */
  3367. static __always_inline bool
  3368. trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
  3369. {
  3370. unsigned int val = cpu_buffer->current_context;
  3371. int bit = interrupt_context_level();
  3372. bit = RB_CTX_NORMAL - bit;
  3373. if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
  3374. /*
  3375. * It is possible that this was called by transitioning
  3376. * between interrupt context, and preempt_count() has not
  3377. * been updated yet. In this case, use the TRANSITION bit.
  3378. */
  3379. bit = RB_CTX_TRANSITION;
  3380. if (val & (1 << (bit + cpu_buffer->nest))) {
  3381. do_ring_buffer_record_recursion();
  3382. return true;
  3383. }
  3384. }
  3385. val |= (1 << (bit + cpu_buffer->nest));
  3386. cpu_buffer->current_context = val;
  3387. return false;
  3388. }
  3389. static __always_inline void
  3390. trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
  3391. {
  3392. cpu_buffer->current_context &=
  3393. cpu_buffer->current_context - (1 << cpu_buffer->nest);
  3394. }
  3395. /* The recursive locking above uses 5 bits */
  3396. #define NESTED_BITS 5
  3397. /**
  3398. * ring_buffer_nest_start - Allow to trace while nested
  3399. * @buffer: The ring buffer to modify
  3400. *
  3401. * The ring buffer has a safety mechanism to prevent recursion.
  3402. * But there may be a case where a trace needs to be done while
  3403. * tracing something else. In this case, calling this function
  3404. * will allow this function to nest within a currently active
  3405. * ring_buffer_lock_reserve().
  3406. *
  3407. * Call this function before calling another ring_buffer_lock_reserve() and
  3408. * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
  3409. */
  3410. void ring_buffer_nest_start(struct trace_buffer *buffer)
  3411. {
  3412. struct ring_buffer_per_cpu *cpu_buffer;
  3413. int cpu;
  3414. /* Enabled by ring_buffer_nest_end() */
  3415. preempt_disable_notrace();
  3416. cpu = raw_smp_processor_id();
  3417. cpu_buffer = buffer->buffers[cpu];
  3418. /* This is the shift value for the above recursive locking */
  3419. cpu_buffer->nest += NESTED_BITS;
  3420. }
  3421. /**
  3422. * ring_buffer_nest_end - Allow to trace while nested
  3423. * @buffer: The ring buffer to modify
  3424. *
  3425. * Must be called after ring_buffer_nest_start() and after the
  3426. * ring_buffer_unlock_commit().
  3427. */
  3428. void ring_buffer_nest_end(struct trace_buffer *buffer)
  3429. {
  3430. struct ring_buffer_per_cpu *cpu_buffer;
  3431. int cpu;
  3432. /* disabled by ring_buffer_nest_start() */
  3433. cpu = raw_smp_processor_id();
  3434. cpu_buffer = buffer->buffers[cpu];
  3435. /* This is the shift value for the above recursive locking */
  3436. cpu_buffer->nest -= NESTED_BITS;
  3437. preempt_enable_notrace();
  3438. }
  3439. /**
  3440. * ring_buffer_unlock_commit - commit a reserved
  3441. * @buffer: The buffer to commit to
  3442. *
  3443. * This commits the data to the ring buffer, and releases any locks held.
  3444. *
  3445. * Must be paired with ring_buffer_lock_reserve.
  3446. */
  3447. int ring_buffer_unlock_commit(struct trace_buffer *buffer)
  3448. {
  3449. struct ring_buffer_per_cpu *cpu_buffer;
  3450. int cpu = raw_smp_processor_id();
  3451. cpu_buffer = buffer->buffers[cpu];
  3452. rb_commit(cpu_buffer);
  3453. rb_wakeups(buffer, cpu_buffer);
  3454. trace_recursive_unlock(cpu_buffer);
  3455. preempt_enable_notrace();
  3456. return 0;
  3457. }
  3458. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  3459. /* Special value to validate all deltas on a page. */
  3460. #define CHECK_FULL_PAGE 1L
  3461. #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
  3462. static const char *show_irq_str(int bits)
  3463. {
  3464. const char *type[] = {
  3465. ".", // 0
  3466. "s", // 1
  3467. "h", // 2
  3468. "Hs", // 3
  3469. "n", // 4
  3470. "Ns", // 5
  3471. "Nh", // 6
  3472. "NHs", // 7
  3473. };
  3474. return type[bits];
  3475. }
  3476. /* Assume this is an trace event */
  3477. static const char *show_flags(struct ring_buffer_event *event)
  3478. {
  3479. struct trace_entry *entry;
  3480. int bits = 0;
  3481. if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
  3482. return "X";
  3483. entry = ring_buffer_event_data(event);
  3484. if (entry->flags & TRACE_FLAG_SOFTIRQ)
  3485. bits |= 1;
  3486. if (entry->flags & TRACE_FLAG_HARDIRQ)
  3487. bits |= 2;
  3488. if (entry->flags & TRACE_FLAG_NMI)
  3489. bits |= 4;
  3490. return show_irq_str(bits);
  3491. }
  3492. static const char *show_irq(struct ring_buffer_event *event)
  3493. {
  3494. struct trace_entry *entry;
  3495. if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry))
  3496. return "";
  3497. entry = ring_buffer_event_data(event);
  3498. if (entry->flags & TRACE_FLAG_IRQS_OFF)
  3499. return "d";
  3500. return "";
  3501. }
  3502. static const char *show_interrupt_level(void)
  3503. {
  3504. unsigned long pc = preempt_count();
  3505. unsigned char level = 0;
  3506. if (pc & SOFTIRQ_OFFSET)
  3507. level |= 1;
  3508. if (pc & HARDIRQ_MASK)
  3509. level |= 2;
  3510. if (pc & NMI_MASK)
  3511. level |= 4;
  3512. return show_irq_str(level);
  3513. }
  3514. static void dump_buffer_page(struct buffer_data_page *bpage,
  3515. struct rb_event_info *info,
  3516. unsigned long tail)
  3517. {
  3518. struct ring_buffer_event *event;
  3519. u64 ts, delta;
  3520. int e;
  3521. ts = bpage->time_stamp;
  3522. pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
  3523. for (e = 0; e < tail; e += rb_event_length(event)) {
  3524. event = (struct ring_buffer_event *)(bpage->data + e);
  3525. switch (event->type_len) {
  3526. case RINGBUF_TYPE_TIME_EXTEND:
  3527. delta = rb_event_time_stamp(event);
  3528. ts += delta;
  3529. pr_warn(" 0x%x: [%lld] delta:%lld TIME EXTEND\n",
  3530. e, ts, delta);
  3531. break;
  3532. case RINGBUF_TYPE_TIME_STAMP:
  3533. delta = rb_event_time_stamp(event);
  3534. ts = rb_fix_abs_ts(delta, ts);
  3535. pr_warn(" 0x%x: [%lld] absolute:%lld TIME STAMP\n",
  3536. e, ts, delta);
  3537. break;
  3538. case RINGBUF_TYPE_PADDING:
  3539. ts += event->time_delta;
  3540. pr_warn(" 0x%x: [%lld] delta:%d PADDING\n",
  3541. e, ts, event->time_delta);
  3542. break;
  3543. case RINGBUF_TYPE_DATA:
  3544. ts += event->time_delta;
  3545. pr_warn(" 0x%x: [%lld] delta:%d %s%s\n",
  3546. e, ts, event->time_delta,
  3547. show_flags(event), show_irq(event));
  3548. break;
  3549. default:
  3550. break;
  3551. }
  3552. }
  3553. pr_warn("expected end:0x%lx last event actually ended at:0x%x\n", tail, e);
  3554. }
  3555. static DEFINE_PER_CPU(atomic_t, checking);
  3556. static atomic_t ts_dump;
  3557. #define buffer_warn_return(fmt, ...) \
  3558. do { \
  3559. /* If another report is happening, ignore this one */ \
  3560. if (atomic_inc_return(&ts_dump) != 1) { \
  3561. atomic_dec(&ts_dump); \
  3562. goto out; \
  3563. } \
  3564. atomic_inc(&cpu_buffer->record_disabled); \
  3565. pr_warn(fmt, ##__VA_ARGS__); \
  3566. dump_buffer_page(bpage, info, tail); \
  3567. atomic_dec(&ts_dump); \
  3568. /* There's some cases in boot up that this can happen */ \
  3569. if (WARN_ON_ONCE(system_state != SYSTEM_BOOTING)) \
  3570. /* Do not re-enable checking */ \
  3571. return; \
  3572. } while (0)
  3573. /*
  3574. * Check if the current event time stamp matches the deltas on
  3575. * the buffer page.
  3576. */
  3577. static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
  3578. struct rb_event_info *info,
  3579. unsigned long tail)
  3580. {
  3581. struct buffer_data_page *bpage;
  3582. u64 ts, delta;
  3583. bool full = false;
  3584. int ret;
  3585. bpage = info->tail_page->page;
  3586. if (tail == CHECK_FULL_PAGE) {
  3587. full = true;
  3588. tail = local_read(&bpage->commit);
  3589. } else if (info->add_timestamp &
  3590. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
  3591. /* Ignore events with absolute time stamps */
  3592. return;
  3593. }
  3594. /*
  3595. * Do not check the first event (skip possible extends too).
  3596. * Also do not check if previous events have not been committed.
  3597. */
  3598. if (tail <= 8 || tail > local_read(&bpage->commit))
  3599. return;
  3600. /*
  3601. * If this interrupted another event,
  3602. */
  3603. if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
  3604. goto out;
  3605. ret = rb_read_data_buffer(bpage, tail, cpu_buffer->cpu, &ts, &delta);
  3606. if (ret < 0) {
  3607. if (delta < ts) {
  3608. buffer_warn_return("[CPU: %d]ABSOLUTE TIME WENT BACKWARDS: last ts: %lld absolute ts: %lld\n",
  3609. cpu_buffer->cpu, ts, delta);
  3610. goto out;
  3611. }
  3612. }
  3613. if ((full && ts > info->ts) ||
  3614. (!full && ts + info->delta != info->ts)) {
  3615. buffer_warn_return("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s context:%s\n",
  3616. cpu_buffer->cpu,
  3617. ts + info->delta, info->ts, info->delta,
  3618. info->before, info->after,
  3619. full ? " (full)" : "", show_interrupt_level());
  3620. }
  3621. out:
  3622. atomic_dec(this_cpu_ptr(&checking));
  3623. }
  3624. #else
  3625. static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
  3626. struct rb_event_info *info,
  3627. unsigned long tail)
  3628. {
  3629. }
  3630. #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
  3631. static struct ring_buffer_event *
  3632. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  3633. struct rb_event_info *info)
  3634. {
  3635. struct ring_buffer_event *event;
  3636. struct buffer_page *tail_page;
  3637. unsigned long tail, write, w;
  3638. /* Don't let the compiler play games with cpu_buffer->tail_page */
  3639. tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
  3640. /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
  3641. barrier();
  3642. rb_time_read(&cpu_buffer->before_stamp, &info->before);
  3643. rb_time_read(&cpu_buffer->write_stamp, &info->after);
  3644. barrier();
  3645. info->ts = rb_time_stamp(cpu_buffer->buffer);
  3646. if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
  3647. info->delta = info->ts;
  3648. } else {
  3649. /*
  3650. * If interrupting an event time update, we may need an
  3651. * absolute timestamp.
  3652. * Don't bother if this is the start of a new page (w == 0).
  3653. */
  3654. if (!w) {
  3655. /* Use the sub-buffer timestamp */
  3656. info->delta = 0;
  3657. } else if (unlikely(info->before != info->after)) {
  3658. info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
  3659. info->length += RB_LEN_TIME_EXTEND;
  3660. } else {
  3661. info->delta = info->ts - info->after;
  3662. if (unlikely(test_time_stamp(info->delta))) {
  3663. info->add_timestamp |= RB_ADD_STAMP_EXTEND;
  3664. info->length += RB_LEN_TIME_EXTEND;
  3665. }
  3666. }
  3667. }
  3668. /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
  3669. /*C*/ write = local_add_return(info->length, &tail_page->write);
  3670. /* set write to only the index of the write */
  3671. write &= RB_WRITE_MASK;
  3672. tail = write - info->length;
  3673. /* See if we shot pass the end of this buffer page */
  3674. if (unlikely(write > cpu_buffer->buffer->subbuf_size)) {
  3675. check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
  3676. return rb_move_tail(cpu_buffer, tail, info);
  3677. }
  3678. if (likely(tail == w)) {
  3679. /* Nothing interrupted us between A and C */
  3680. /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
  3681. /*
  3682. * If something came in between C and D, the write stamp
  3683. * may now not be in sync. But that's fine as the before_stamp
  3684. * will be different and then next event will just be forced
  3685. * to use an absolute timestamp.
  3686. */
  3687. if (likely(!(info->add_timestamp &
  3688. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
  3689. /* This did not interrupt any time update */
  3690. info->delta = info->ts - info->after;
  3691. else
  3692. /* Just use full timestamp for interrupting event */
  3693. info->delta = info->ts;
  3694. check_buffer(cpu_buffer, info, tail);
  3695. } else {
  3696. u64 ts;
  3697. /* SLOW PATH - Interrupted between A and C */
  3698. /* Save the old before_stamp */
  3699. rb_time_read(&cpu_buffer->before_stamp, &info->before);
  3700. /*
  3701. * Read a new timestamp and update the before_stamp to make
  3702. * the next event after this one force using an absolute
  3703. * timestamp. This is in case an interrupt were to come in
  3704. * between E and F.
  3705. */
  3706. ts = rb_time_stamp(cpu_buffer->buffer);
  3707. rb_time_set(&cpu_buffer->before_stamp, ts);
  3708. barrier();
  3709. /*E*/ rb_time_read(&cpu_buffer->write_stamp, &info->after);
  3710. barrier();
  3711. /*F*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
  3712. info->after == info->before && info->after < ts) {
  3713. /*
  3714. * Nothing came after this event between C and F, it is
  3715. * safe to use info->after for the delta as it
  3716. * matched info->before and is still valid.
  3717. */
  3718. info->delta = ts - info->after;
  3719. } else {
  3720. /*
  3721. * Interrupted between C and F:
  3722. * Lost the previous events time stamp. Just set the
  3723. * delta to zero, and this will be the same time as
  3724. * the event this event interrupted. And the events that
  3725. * came after this will still be correct (as they would
  3726. * have built their delta on the previous event.
  3727. */
  3728. info->delta = 0;
  3729. }
  3730. info->ts = ts;
  3731. info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
  3732. }
  3733. /*
  3734. * If this is the first commit on the page, then it has the same
  3735. * timestamp as the page itself.
  3736. */
  3737. if (unlikely(!tail && !(info->add_timestamp &
  3738. (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
  3739. info->delta = 0;
  3740. /* We reserved something on the buffer */
  3741. event = __rb_page_index(tail_page, tail);
  3742. rb_update_event(cpu_buffer, event, info);
  3743. local_inc(&tail_page->entries);
  3744. /*
  3745. * If this is the first commit on the page, then update
  3746. * its timestamp.
  3747. */
  3748. if (unlikely(!tail))
  3749. tail_page->page->time_stamp = info->ts;
  3750. /* account for these added bytes */
  3751. local_add(info->length, &cpu_buffer->entries_bytes);
  3752. return event;
  3753. }
  3754. static __always_inline struct ring_buffer_event *
  3755. rb_reserve_next_event(struct trace_buffer *buffer,
  3756. struct ring_buffer_per_cpu *cpu_buffer,
  3757. unsigned long length)
  3758. {
  3759. struct ring_buffer_event *event;
  3760. struct rb_event_info info;
  3761. int nr_loops = 0;
  3762. int add_ts_default;
  3763. /*
  3764. * ring buffer does cmpxchg as well as atomic64 operations
  3765. * (which some archs use locking for atomic64), make sure this
  3766. * is safe in NMI context
  3767. */
  3768. if ((!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) ||
  3769. IS_ENABLED(CONFIG_GENERIC_ATOMIC64)) &&
  3770. (unlikely(in_nmi()))) {
  3771. return NULL;
  3772. }
  3773. rb_start_commit(cpu_buffer);
  3774. /* The commit page can not change after this */
  3775. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3776. /*
  3777. * Due to the ability to swap a cpu buffer from a buffer
  3778. * it is possible it was swapped before we committed.
  3779. * (committing stops a swap). We check for it here and
  3780. * if it happened, we have to fail the write.
  3781. */
  3782. barrier();
  3783. if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
  3784. local_dec(&cpu_buffer->committing);
  3785. local_dec(&cpu_buffer->commits);
  3786. return NULL;
  3787. }
  3788. #endif
  3789. info.length = rb_calculate_event_length(length);
  3790. if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
  3791. add_ts_default = RB_ADD_STAMP_ABSOLUTE;
  3792. info.length += RB_LEN_TIME_EXTEND;
  3793. if (info.length > cpu_buffer->buffer->max_data_size)
  3794. goto out_fail;
  3795. } else {
  3796. add_ts_default = RB_ADD_STAMP_NONE;
  3797. }
  3798. again:
  3799. info.add_timestamp = add_ts_default;
  3800. info.delta = 0;
  3801. /*
  3802. * We allow for interrupts to reenter here and do a trace.
  3803. * If one does, it will cause this original code to loop
  3804. * back here. Even with heavy interrupts happening, this
  3805. * should only happen a few times in a row. If this happens
  3806. * 1000 times in a row, there must be either an interrupt
  3807. * storm or we have something buggy.
  3808. * Bail!
  3809. */
  3810. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  3811. goto out_fail;
  3812. event = __rb_reserve_next(cpu_buffer, &info);
  3813. if (unlikely(PTR_ERR(event) == -EAGAIN)) {
  3814. if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
  3815. info.length -= RB_LEN_TIME_EXTEND;
  3816. goto again;
  3817. }
  3818. if (likely(event))
  3819. return event;
  3820. out_fail:
  3821. rb_end_commit(cpu_buffer);
  3822. return NULL;
  3823. }
  3824. /**
  3825. * ring_buffer_lock_reserve - reserve a part of the buffer
  3826. * @buffer: the ring buffer to reserve from
  3827. * @length: the length of the data to reserve (excluding event header)
  3828. *
  3829. * Returns a reserved event on the ring buffer to copy directly to.
  3830. * The user of this interface will need to get the body to write into
  3831. * and can use the ring_buffer_event_data() interface.
  3832. *
  3833. * The length is the length of the data needed, not the event length
  3834. * which also includes the event header.
  3835. *
  3836. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  3837. * If NULL is returned, then nothing has been allocated or locked.
  3838. */
  3839. struct ring_buffer_event *
  3840. ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
  3841. {
  3842. struct ring_buffer_per_cpu *cpu_buffer;
  3843. struct ring_buffer_event *event;
  3844. int cpu;
  3845. /* If we are tracing schedule, we don't want to recurse */
  3846. preempt_disable_notrace();
  3847. if (unlikely(atomic_read(&buffer->record_disabled)))
  3848. goto out;
  3849. cpu = raw_smp_processor_id();
  3850. if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
  3851. goto out;
  3852. cpu_buffer = buffer->buffers[cpu];
  3853. if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
  3854. goto out;
  3855. if (unlikely(length > buffer->max_data_size))
  3856. goto out;
  3857. if (unlikely(trace_recursive_lock(cpu_buffer)))
  3858. goto out;
  3859. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  3860. if (!event)
  3861. goto out_unlock;
  3862. return event;
  3863. out_unlock:
  3864. trace_recursive_unlock(cpu_buffer);
  3865. out:
  3866. preempt_enable_notrace();
  3867. return NULL;
  3868. }
  3869. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  3870. /*
  3871. * Decrement the entries to the page that an event is on.
  3872. * The event does not even need to exist, only the pointer
  3873. * to the page it is on. This may only be called before the commit
  3874. * takes place.
  3875. */
  3876. static inline void
  3877. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  3878. struct ring_buffer_event *event)
  3879. {
  3880. unsigned long addr = (unsigned long)event;
  3881. struct buffer_page *bpage = cpu_buffer->commit_page;
  3882. struct buffer_page *start;
  3883. addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1);
  3884. /* Do the likely case first */
  3885. if (likely(bpage->page == (void *)addr)) {
  3886. local_dec(&bpage->entries);
  3887. return;
  3888. }
  3889. /*
  3890. * Because the commit page may be on the reader page we
  3891. * start with the next page and check the end loop there.
  3892. */
  3893. rb_inc_page(&bpage);
  3894. start = bpage;
  3895. do {
  3896. if (bpage->page == (void *)addr) {
  3897. local_dec(&bpage->entries);
  3898. return;
  3899. }
  3900. rb_inc_page(&bpage);
  3901. } while (bpage != start);
  3902. /* commit not part of this buffer?? */
  3903. RB_WARN_ON(cpu_buffer, 1);
  3904. }
  3905. /**
  3906. * ring_buffer_discard_commit - discard an event that has not been committed
  3907. * @buffer: the ring buffer
  3908. * @event: non committed event to discard
  3909. *
  3910. * Sometimes an event that is in the ring buffer needs to be ignored.
  3911. * This function lets the user discard an event in the ring buffer
  3912. * and then that event will not be read later.
  3913. *
  3914. * This function only works if it is called before the item has been
  3915. * committed. It will try to free the event from the ring buffer
  3916. * if another event has not been added behind it.
  3917. *
  3918. * If another event has been added behind it, it will set the event
  3919. * up as discarded, and perform the commit.
  3920. *
  3921. * If this function is called, do not call ring_buffer_unlock_commit on
  3922. * the event.
  3923. */
  3924. void ring_buffer_discard_commit(struct trace_buffer *buffer,
  3925. struct ring_buffer_event *event)
  3926. {
  3927. struct ring_buffer_per_cpu *cpu_buffer;
  3928. int cpu;
  3929. /* The event is discarded regardless */
  3930. rb_event_discard(event);
  3931. cpu = smp_processor_id();
  3932. cpu_buffer = buffer->buffers[cpu];
  3933. /*
  3934. * This must only be called if the event has not been
  3935. * committed yet. Thus we can assume that preemption
  3936. * is still disabled.
  3937. */
  3938. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  3939. rb_decrement_entry(cpu_buffer, event);
  3940. if (rb_try_to_discard(cpu_buffer, event))
  3941. goto out;
  3942. out:
  3943. rb_end_commit(cpu_buffer);
  3944. trace_recursive_unlock(cpu_buffer);
  3945. preempt_enable_notrace();
  3946. }
  3947. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  3948. /**
  3949. * ring_buffer_write - write data to the buffer without reserving
  3950. * @buffer: The ring buffer to write to.
  3951. * @length: The length of the data being written (excluding the event header)
  3952. * @data: The data to write to the buffer.
  3953. *
  3954. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  3955. * one function. If you already have the data to write to the buffer, it
  3956. * may be easier to simply call this function.
  3957. *
  3958. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  3959. * and not the length of the event which would hold the header.
  3960. */
  3961. int ring_buffer_write(struct trace_buffer *buffer,
  3962. unsigned long length,
  3963. void *data)
  3964. {
  3965. struct ring_buffer_per_cpu *cpu_buffer;
  3966. struct ring_buffer_event *event;
  3967. void *body;
  3968. int ret = -EBUSY;
  3969. int cpu;
  3970. preempt_disable_notrace();
  3971. if (atomic_read(&buffer->record_disabled))
  3972. goto out;
  3973. cpu = raw_smp_processor_id();
  3974. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3975. goto out;
  3976. cpu_buffer = buffer->buffers[cpu];
  3977. if (atomic_read(&cpu_buffer->record_disabled))
  3978. goto out;
  3979. if (length > buffer->max_data_size)
  3980. goto out;
  3981. if (unlikely(trace_recursive_lock(cpu_buffer)))
  3982. goto out;
  3983. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  3984. if (!event)
  3985. goto out_unlock;
  3986. body = rb_event_data(event);
  3987. memcpy(body, data, length);
  3988. rb_commit(cpu_buffer);
  3989. rb_wakeups(buffer, cpu_buffer);
  3990. ret = 0;
  3991. out_unlock:
  3992. trace_recursive_unlock(cpu_buffer);
  3993. out:
  3994. preempt_enable_notrace();
  3995. return ret;
  3996. }
  3997. EXPORT_SYMBOL_GPL(ring_buffer_write);
  3998. static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  3999. {
  4000. struct buffer_page *reader = cpu_buffer->reader_page;
  4001. struct buffer_page *head = rb_set_head_page(cpu_buffer);
  4002. struct buffer_page *commit = cpu_buffer->commit_page;
  4003. /* In case of error, head will be NULL */
  4004. if (unlikely(!head))
  4005. return true;
  4006. /* Reader should exhaust content in reader page */
  4007. if (reader->read != rb_page_size(reader))
  4008. return false;
  4009. /*
  4010. * If writers are committing on the reader page, knowing all
  4011. * committed content has been read, the ring buffer is empty.
  4012. */
  4013. if (commit == reader)
  4014. return true;
  4015. /*
  4016. * If writers are committing on a page other than reader page
  4017. * and head page, there should always be content to read.
  4018. */
  4019. if (commit != head)
  4020. return false;
  4021. /*
  4022. * Writers are committing on the head page, we just need
  4023. * to care about there're committed data, and the reader will
  4024. * swap reader page with head page when it is to read data.
  4025. */
  4026. return rb_page_commit(commit) == 0;
  4027. }
  4028. /**
  4029. * ring_buffer_record_disable - stop all writes into the buffer
  4030. * @buffer: The ring buffer to stop writes to.
  4031. *
  4032. * This prevents all writes to the buffer. Any attempt to write
  4033. * to the buffer after this will fail and return NULL.
  4034. *
  4035. * The caller should call synchronize_rcu() after this.
  4036. */
  4037. void ring_buffer_record_disable(struct trace_buffer *buffer)
  4038. {
  4039. atomic_inc(&buffer->record_disabled);
  4040. }
  4041. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  4042. /**
  4043. * ring_buffer_record_enable - enable writes to the buffer
  4044. * @buffer: The ring buffer to enable writes
  4045. *
  4046. * Note, multiple disables will need the same number of enables
  4047. * to truly enable the writing (much like preempt_disable).
  4048. */
  4049. void ring_buffer_record_enable(struct trace_buffer *buffer)
  4050. {
  4051. atomic_dec(&buffer->record_disabled);
  4052. }
  4053. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  4054. /**
  4055. * ring_buffer_record_off - stop all writes into the buffer
  4056. * @buffer: The ring buffer to stop writes to.
  4057. *
  4058. * This prevents all writes to the buffer. Any attempt to write
  4059. * to the buffer after this will fail and return NULL.
  4060. *
  4061. * This is different than ring_buffer_record_disable() as
  4062. * it works like an on/off switch, where as the disable() version
  4063. * must be paired with a enable().
  4064. */
  4065. void ring_buffer_record_off(struct trace_buffer *buffer)
  4066. {
  4067. unsigned int rd;
  4068. unsigned int new_rd;
  4069. rd = atomic_read(&buffer->record_disabled);
  4070. do {
  4071. new_rd = rd | RB_BUFFER_OFF;
  4072. } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
  4073. }
  4074. EXPORT_SYMBOL_GPL(ring_buffer_record_off);
  4075. /**
  4076. * ring_buffer_record_on - restart writes into the buffer
  4077. * @buffer: The ring buffer to start writes to.
  4078. *
  4079. * This enables all writes to the buffer that was disabled by
  4080. * ring_buffer_record_off().
  4081. *
  4082. * This is different than ring_buffer_record_enable() as
  4083. * it works like an on/off switch, where as the enable() version
  4084. * must be paired with a disable().
  4085. */
  4086. void ring_buffer_record_on(struct trace_buffer *buffer)
  4087. {
  4088. unsigned int rd;
  4089. unsigned int new_rd;
  4090. rd = atomic_read(&buffer->record_disabled);
  4091. do {
  4092. new_rd = rd & ~RB_BUFFER_OFF;
  4093. } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd));
  4094. }
  4095. EXPORT_SYMBOL_GPL(ring_buffer_record_on);
  4096. /**
  4097. * ring_buffer_record_is_on - return true if the ring buffer can write
  4098. * @buffer: The ring buffer to see if write is enabled
  4099. *
  4100. * Returns true if the ring buffer is in a state that it accepts writes.
  4101. */
  4102. bool ring_buffer_record_is_on(struct trace_buffer *buffer)
  4103. {
  4104. return !atomic_read(&buffer->record_disabled);
  4105. }
  4106. /**
  4107. * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
  4108. * @buffer: The ring buffer to see if write is set enabled
  4109. *
  4110. * Returns true if the ring buffer is set writable by ring_buffer_record_on().
  4111. * Note that this does NOT mean it is in a writable state.
  4112. *
  4113. * It may return true when the ring buffer has been disabled by
  4114. * ring_buffer_record_disable(), as that is a temporary disabling of
  4115. * the ring buffer.
  4116. */
  4117. bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
  4118. {
  4119. return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
  4120. }
  4121. /**
  4122. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  4123. * @buffer: The ring buffer to stop writes to.
  4124. * @cpu: The CPU buffer to stop
  4125. *
  4126. * This prevents all writes to the buffer. Any attempt to write
  4127. * to the buffer after this will fail and return NULL.
  4128. *
  4129. * The caller should call synchronize_rcu() after this.
  4130. */
  4131. void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
  4132. {
  4133. struct ring_buffer_per_cpu *cpu_buffer;
  4134. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4135. return;
  4136. cpu_buffer = buffer->buffers[cpu];
  4137. atomic_inc(&cpu_buffer->record_disabled);
  4138. }
  4139. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  4140. /**
  4141. * ring_buffer_record_enable_cpu - enable writes to the buffer
  4142. * @buffer: The ring buffer to enable writes
  4143. * @cpu: The CPU to enable.
  4144. *
  4145. * Note, multiple disables will need the same number of enables
  4146. * to truly enable the writing (much like preempt_disable).
  4147. */
  4148. void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
  4149. {
  4150. struct ring_buffer_per_cpu *cpu_buffer;
  4151. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4152. return;
  4153. cpu_buffer = buffer->buffers[cpu];
  4154. atomic_dec(&cpu_buffer->record_disabled);
  4155. }
  4156. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  4157. /*
  4158. * The total entries in the ring buffer is the running counter
  4159. * of entries entered into the ring buffer, minus the sum of
  4160. * the entries read from the ring buffer and the number of
  4161. * entries that were overwritten.
  4162. */
  4163. static inline unsigned long
  4164. rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
  4165. {
  4166. return local_read(&cpu_buffer->entries) -
  4167. (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
  4168. }
  4169. /**
  4170. * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
  4171. * @buffer: The ring buffer
  4172. * @cpu: The per CPU buffer to read from.
  4173. */
  4174. u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
  4175. {
  4176. unsigned long flags;
  4177. struct ring_buffer_per_cpu *cpu_buffer;
  4178. struct buffer_page *bpage;
  4179. u64 ret = 0;
  4180. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4181. return 0;
  4182. cpu_buffer = buffer->buffers[cpu];
  4183. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4184. /*
  4185. * if the tail is on reader_page, oldest time stamp is on the reader
  4186. * page
  4187. */
  4188. if (cpu_buffer->tail_page == cpu_buffer->reader_page)
  4189. bpage = cpu_buffer->reader_page;
  4190. else
  4191. bpage = rb_set_head_page(cpu_buffer);
  4192. if (bpage)
  4193. ret = bpage->page->time_stamp;
  4194. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  4195. return ret;
  4196. }
  4197. EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
  4198. /**
  4199. * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer
  4200. * @buffer: The ring buffer
  4201. * @cpu: The per CPU buffer to read from.
  4202. */
  4203. unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
  4204. {
  4205. struct ring_buffer_per_cpu *cpu_buffer;
  4206. unsigned long ret;
  4207. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4208. return 0;
  4209. cpu_buffer = buffer->buffers[cpu];
  4210. ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
  4211. return ret;
  4212. }
  4213. EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
  4214. /**
  4215. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  4216. * @buffer: The ring buffer
  4217. * @cpu: The per CPU buffer to get the entries from.
  4218. */
  4219. unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
  4220. {
  4221. struct ring_buffer_per_cpu *cpu_buffer;
  4222. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4223. return 0;
  4224. cpu_buffer = buffer->buffers[cpu];
  4225. return rb_num_of_entries(cpu_buffer);
  4226. }
  4227. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  4228. /**
  4229. * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
  4230. * buffer wrapping around (only if RB_FL_OVERWRITE is on).
  4231. * @buffer: The ring buffer
  4232. * @cpu: The per CPU buffer to get the number of overruns from
  4233. */
  4234. unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
  4235. {
  4236. struct ring_buffer_per_cpu *cpu_buffer;
  4237. unsigned long ret;
  4238. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4239. return 0;
  4240. cpu_buffer = buffer->buffers[cpu];
  4241. ret = local_read(&cpu_buffer->overrun);
  4242. return ret;
  4243. }
  4244. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  4245. /**
  4246. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
  4247. * commits failing due to the buffer wrapping around while there are uncommitted
  4248. * events, such as during an interrupt storm.
  4249. * @buffer: The ring buffer
  4250. * @cpu: The per CPU buffer to get the number of overruns from
  4251. */
  4252. unsigned long
  4253. ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
  4254. {
  4255. struct ring_buffer_per_cpu *cpu_buffer;
  4256. unsigned long ret;
  4257. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4258. return 0;
  4259. cpu_buffer = buffer->buffers[cpu];
  4260. ret = local_read(&cpu_buffer->commit_overrun);
  4261. return ret;
  4262. }
  4263. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  4264. /**
  4265. * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
  4266. * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
  4267. * @buffer: The ring buffer
  4268. * @cpu: The per CPU buffer to get the number of overruns from
  4269. */
  4270. unsigned long
  4271. ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
  4272. {
  4273. struct ring_buffer_per_cpu *cpu_buffer;
  4274. unsigned long ret;
  4275. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4276. return 0;
  4277. cpu_buffer = buffer->buffers[cpu];
  4278. ret = local_read(&cpu_buffer->dropped_events);
  4279. return ret;
  4280. }
  4281. EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
  4282. /**
  4283. * ring_buffer_read_events_cpu - get the number of events successfully read
  4284. * @buffer: The ring buffer
  4285. * @cpu: The per CPU buffer to get the number of events read
  4286. */
  4287. unsigned long
  4288. ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
  4289. {
  4290. struct ring_buffer_per_cpu *cpu_buffer;
  4291. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4292. return 0;
  4293. cpu_buffer = buffer->buffers[cpu];
  4294. return cpu_buffer->read;
  4295. }
  4296. EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
  4297. /**
  4298. * ring_buffer_entries - get the number of entries in a buffer
  4299. * @buffer: The ring buffer
  4300. *
  4301. * Returns the total number of entries in the ring buffer
  4302. * (all CPU entries)
  4303. */
  4304. unsigned long ring_buffer_entries(struct trace_buffer *buffer)
  4305. {
  4306. struct ring_buffer_per_cpu *cpu_buffer;
  4307. unsigned long entries = 0;
  4308. int cpu;
  4309. /* if you care about this being correct, lock the buffer */
  4310. for_each_buffer_cpu(buffer, cpu) {
  4311. cpu_buffer = buffer->buffers[cpu];
  4312. entries += rb_num_of_entries(cpu_buffer);
  4313. }
  4314. return entries;
  4315. }
  4316. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  4317. /**
  4318. * ring_buffer_overruns - get the number of overruns in buffer
  4319. * @buffer: The ring buffer
  4320. *
  4321. * Returns the total number of overruns in the ring buffer
  4322. * (all CPU entries)
  4323. */
  4324. unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
  4325. {
  4326. struct ring_buffer_per_cpu *cpu_buffer;
  4327. unsigned long overruns = 0;
  4328. int cpu;
  4329. /* if you care about this being correct, lock the buffer */
  4330. for_each_buffer_cpu(buffer, cpu) {
  4331. cpu_buffer = buffer->buffers[cpu];
  4332. overruns += local_read(&cpu_buffer->overrun);
  4333. }
  4334. return overruns;
  4335. }
  4336. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  4337. static void rb_iter_reset(struct ring_buffer_iter *iter)
  4338. {
  4339. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  4340. /* Iterator usage is expected to have record disabled */
  4341. iter->head_page = cpu_buffer->reader_page;
  4342. iter->head = cpu_buffer->reader_page->read;
  4343. iter->next_event = iter->head;
  4344. iter->cache_reader_page = iter->head_page;
  4345. iter->cache_read = cpu_buffer->read;
  4346. iter->cache_pages_removed = cpu_buffer->pages_removed;
  4347. if (iter->head) {
  4348. iter->read_stamp = cpu_buffer->read_stamp;
  4349. iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
  4350. } else {
  4351. iter->read_stamp = iter->head_page->page->time_stamp;
  4352. iter->page_stamp = iter->read_stamp;
  4353. }
  4354. }
  4355. /**
  4356. * ring_buffer_iter_reset - reset an iterator
  4357. * @iter: The iterator to reset
  4358. *
  4359. * Resets the iterator, so that it will start from the beginning
  4360. * again.
  4361. */
  4362. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  4363. {
  4364. struct ring_buffer_per_cpu *cpu_buffer;
  4365. unsigned long flags;
  4366. if (!iter)
  4367. return;
  4368. cpu_buffer = iter->cpu_buffer;
  4369. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4370. rb_iter_reset(iter);
  4371. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  4372. }
  4373. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  4374. /**
  4375. * ring_buffer_iter_empty - check if an iterator has no more to read
  4376. * @iter: The iterator to check
  4377. */
  4378. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  4379. {
  4380. struct ring_buffer_per_cpu *cpu_buffer;
  4381. struct buffer_page *reader;
  4382. struct buffer_page *head_page;
  4383. struct buffer_page *commit_page;
  4384. struct buffer_page *curr_commit_page;
  4385. unsigned commit;
  4386. u64 curr_commit_ts;
  4387. u64 commit_ts;
  4388. cpu_buffer = iter->cpu_buffer;
  4389. reader = cpu_buffer->reader_page;
  4390. head_page = cpu_buffer->head_page;
  4391. commit_page = READ_ONCE(cpu_buffer->commit_page);
  4392. commit_ts = commit_page->page->time_stamp;
  4393. /*
  4394. * When the writer goes across pages, it issues a cmpxchg which
  4395. * is a mb(), which will synchronize with the rmb here.
  4396. * (see rb_tail_page_update())
  4397. */
  4398. smp_rmb();
  4399. commit = rb_page_commit(commit_page);
  4400. /* We want to make sure that the commit page doesn't change */
  4401. smp_rmb();
  4402. /* Make sure commit page didn't change */
  4403. curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
  4404. curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
  4405. /* If the commit page changed, then there's more data */
  4406. if (curr_commit_page != commit_page ||
  4407. curr_commit_ts != commit_ts)
  4408. return 0;
  4409. /* Still racy, as it may return a false positive, but that's OK */
  4410. return ((iter->head_page == commit_page && iter->head >= commit) ||
  4411. (iter->head_page == reader && commit_page == head_page &&
  4412. head_page->read == commit &&
  4413. iter->head == rb_page_size(cpu_buffer->reader_page)));
  4414. }
  4415. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  4416. static void
  4417. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  4418. struct ring_buffer_event *event)
  4419. {
  4420. u64 delta;
  4421. switch (event->type_len) {
  4422. case RINGBUF_TYPE_PADDING:
  4423. return;
  4424. case RINGBUF_TYPE_TIME_EXTEND:
  4425. delta = rb_event_time_stamp(event);
  4426. cpu_buffer->read_stamp += delta;
  4427. return;
  4428. case RINGBUF_TYPE_TIME_STAMP:
  4429. delta = rb_event_time_stamp(event);
  4430. delta = rb_fix_abs_ts(delta, cpu_buffer->read_stamp);
  4431. cpu_buffer->read_stamp = delta;
  4432. return;
  4433. case RINGBUF_TYPE_DATA:
  4434. cpu_buffer->read_stamp += event->time_delta;
  4435. return;
  4436. default:
  4437. RB_WARN_ON(cpu_buffer, 1);
  4438. }
  4439. }
  4440. static void
  4441. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  4442. struct ring_buffer_event *event)
  4443. {
  4444. u64 delta;
  4445. switch (event->type_len) {
  4446. case RINGBUF_TYPE_PADDING:
  4447. return;
  4448. case RINGBUF_TYPE_TIME_EXTEND:
  4449. delta = rb_event_time_stamp(event);
  4450. iter->read_stamp += delta;
  4451. return;
  4452. case RINGBUF_TYPE_TIME_STAMP:
  4453. delta = rb_event_time_stamp(event);
  4454. delta = rb_fix_abs_ts(delta, iter->read_stamp);
  4455. iter->read_stamp = delta;
  4456. return;
  4457. case RINGBUF_TYPE_DATA:
  4458. iter->read_stamp += event->time_delta;
  4459. return;
  4460. default:
  4461. RB_WARN_ON(iter->cpu_buffer, 1);
  4462. }
  4463. }
  4464. static struct buffer_page *
  4465. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  4466. {
  4467. struct buffer_page *reader = NULL;
  4468. unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size);
  4469. unsigned long overwrite;
  4470. unsigned long flags;
  4471. int nr_loops = 0;
  4472. bool ret;
  4473. local_irq_save(flags);
  4474. arch_spin_lock(&cpu_buffer->lock);
  4475. again:
  4476. /*
  4477. * This should normally only loop twice. But because the
  4478. * start of the reader inserts an empty page, it causes
  4479. * a case where we will loop three times. There should be no
  4480. * reason to loop four times (that I know of).
  4481. */
  4482. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  4483. reader = NULL;
  4484. goto out;
  4485. }
  4486. reader = cpu_buffer->reader_page;
  4487. /* If there's more to read, return this page */
  4488. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  4489. goto out;
  4490. /* Never should we have an index greater than the size */
  4491. if (RB_WARN_ON(cpu_buffer,
  4492. cpu_buffer->reader_page->read > rb_page_size(reader)))
  4493. goto out;
  4494. /* check if we caught up to the tail */
  4495. reader = NULL;
  4496. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  4497. goto out;
  4498. /* Don't bother swapping if the ring buffer is empty */
  4499. if (rb_num_of_entries(cpu_buffer) == 0)
  4500. goto out;
  4501. /*
  4502. * Reset the reader page to size zero.
  4503. */
  4504. local_set(&cpu_buffer->reader_page->write, 0);
  4505. local_set(&cpu_buffer->reader_page->entries, 0);
  4506. local_set(&cpu_buffer->reader_page->page->commit, 0);
  4507. cpu_buffer->reader_page->real_end = 0;
  4508. spin:
  4509. /*
  4510. * Splice the empty reader page into the list around the head.
  4511. */
  4512. reader = rb_set_head_page(cpu_buffer);
  4513. if (!reader)
  4514. goto out;
  4515. cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
  4516. cpu_buffer->reader_page->list.prev = reader->list.prev;
  4517. /*
  4518. * cpu_buffer->pages just needs to point to the buffer, it
  4519. * has no specific buffer page to point to. Lets move it out
  4520. * of our way so we don't accidentally swap it.
  4521. */
  4522. cpu_buffer->pages = reader->list.prev;
  4523. /* The reader page will be pointing to the new head */
  4524. rb_set_list_to_head(&cpu_buffer->reader_page->list);
  4525. /*
  4526. * We want to make sure we read the overruns after we set up our
  4527. * pointers to the next object. The writer side does a
  4528. * cmpxchg to cross pages which acts as the mb on the writer
  4529. * side. Note, the reader will constantly fail the swap
  4530. * while the writer is updating the pointers, so this
  4531. * guarantees that the overwrite recorded here is the one we
  4532. * want to compare with the last_overrun.
  4533. */
  4534. smp_mb();
  4535. overwrite = local_read(&(cpu_buffer->overrun));
  4536. /*
  4537. * Here's the tricky part.
  4538. *
  4539. * We need to move the pointer past the header page.
  4540. * But we can only do that if a writer is not currently
  4541. * moving it. The page before the header page has the
  4542. * flag bit '1' set if it is pointing to the page we want.
  4543. * but if the writer is in the process of moving it
  4544. * than it will be '2' or already moved '0'.
  4545. */
  4546. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  4547. /*
  4548. * If we did not convert it, then we must try again.
  4549. */
  4550. if (!ret)
  4551. goto spin;
  4552. if (cpu_buffer->ring_meta)
  4553. rb_update_meta_reader(cpu_buffer, reader);
  4554. /*
  4555. * Yay! We succeeded in replacing the page.
  4556. *
  4557. * Now make the new head point back to the reader page.
  4558. */
  4559. rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
  4560. rb_inc_page(&cpu_buffer->head_page);
  4561. cpu_buffer->cnt++;
  4562. local_inc(&cpu_buffer->pages_read);
  4563. /* Finally update the reader page to the new head */
  4564. cpu_buffer->reader_page = reader;
  4565. cpu_buffer->reader_page->read = 0;
  4566. if (overwrite != cpu_buffer->last_overrun) {
  4567. cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
  4568. cpu_buffer->last_overrun = overwrite;
  4569. }
  4570. goto again;
  4571. out:
  4572. /* Update the read_stamp on the first event */
  4573. if (reader && reader->read == 0)
  4574. cpu_buffer->read_stamp = reader->page->time_stamp;
  4575. arch_spin_unlock(&cpu_buffer->lock);
  4576. local_irq_restore(flags);
  4577. /*
  4578. * The writer has preempt disable, wait for it. But not forever
  4579. * Although, 1 second is pretty much "forever"
  4580. */
  4581. #define USECS_WAIT 1000000
  4582. for (nr_loops = 0; nr_loops < USECS_WAIT; nr_loops++) {
  4583. /* If the write is past the end of page, a writer is still updating it */
  4584. if (likely(!reader || rb_page_write(reader) <= bsize))
  4585. break;
  4586. udelay(1);
  4587. /* Get the latest version of the reader write value */
  4588. smp_rmb();
  4589. }
  4590. /* The writer is not moving forward? Something is wrong */
  4591. if (RB_WARN_ON(cpu_buffer, nr_loops == USECS_WAIT))
  4592. reader = NULL;
  4593. /*
  4594. * Make sure we see any padding after the write update
  4595. * (see rb_reset_tail()).
  4596. *
  4597. * In addition, a writer may be writing on the reader page
  4598. * if the page has not been fully filled, so the read barrier
  4599. * is also needed to make sure we see the content of what is
  4600. * committed by the writer (see rb_set_commit_to_write()).
  4601. */
  4602. smp_rmb();
  4603. return reader;
  4604. }
  4605. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  4606. {
  4607. struct ring_buffer_event *event;
  4608. struct buffer_page *reader;
  4609. unsigned length;
  4610. reader = rb_get_reader_page(cpu_buffer);
  4611. /* This function should not be called when buffer is empty */
  4612. if (RB_WARN_ON(cpu_buffer, !reader))
  4613. return;
  4614. event = rb_reader_event(cpu_buffer);
  4615. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  4616. cpu_buffer->read++;
  4617. rb_update_read_stamp(cpu_buffer, event);
  4618. length = rb_event_length(event);
  4619. cpu_buffer->reader_page->read += length;
  4620. cpu_buffer->read_bytes += length;
  4621. }
  4622. static void rb_advance_iter(struct ring_buffer_iter *iter)
  4623. {
  4624. struct ring_buffer_per_cpu *cpu_buffer;
  4625. cpu_buffer = iter->cpu_buffer;
  4626. /* If head == next_event then we need to jump to the next event */
  4627. if (iter->head == iter->next_event) {
  4628. /* If the event gets overwritten again, there's nothing to do */
  4629. if (rb_iter_head_event(iter) == NULL)
  4630. return;
  4631. }
  4632. iter->head = iter->next_event;
  4633. /*
  4634. * Check if we are at the end of the buffer.
  4635. */
  4636. if (iter->next_event >= rb_page_size(iter->head_page)) {
  4637. /* discarded commits can make the page empty */
  4638. if (iter->head_page == cpu_buffer->commit_page)
  4639. return;
  4640. rb_inc_iter(iter);
  4641. return;
  4642. }
  4643. rb_update_iter_read_stamp(iter, iter->event);
  4644. }
  4645. static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
  4646. {
  4647. return cpu_buffer->lost_events;
  4648. }
  4649. static struct ring_buffer_event *
  4650. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
  4651. unsigned long *lost_events)
  4652. {
  4653. struct ring_buffer_event *event;
  4654. struct buffer_page *reader;
  4655. int nr_loops = 0;
  4656. if (ts)
  4657. *ts = 0;
  4658. again:
  4659. /*
  4660. * We repeat when a time extend is encountered.
  4661. * Since the time extend is always attached to a data event,
  4662. * we should never loop more than once.
  4663. * (We never hit the following condition more than twice).
  4664. */
  4665. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
  4666. return NULL;
  4667. reader = rb_get_reader_page(cpu_buffer);
  4668. if (!reader)
  4669. return NULL;
  4670. event = rb_reader_event(cpu_buffer);
  4671. switch (event->type_len) {
  4672. case RINGBUF_TYPE_PADDING:
  4673. if (rb_null_event(event))
  4674. RB_WARN_ON(cpu_buffer, 1);
  4675. /*
  4676. * Because the writer could be discarding every
  4677. * event it creates (which would probably be bad)
  4678. * if we were to go back to "again" then we may never
  4679. * catch up, and will trigger the warn on, or lock
  4680. * the box. Return the padding, and we will release
  4681. * the current locks, and try again.
  4682. */
  4683. return event;
  4684. case RINGBUF_TYPE_TIME_EXTEND:
  4685. /* Internal data, OK to advance */
  4686. rb_advance_reader(cpu_buffer);
  4687. goto again;
  4688. case RINGBUF_TYPE_TIME_STAMP:
  4689. if (ts) {
  4690. *ts = rb_event_time_stamp(event);
  4691. *ts = rb_fix_abs_ts(*ts, reader->page->time_stamp);
  4692. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4693. cpu_buffer->cpu, ts);
  4694. }
  4695. /* Internal data, OK to advance */
  4696. rb_advance_reader(cpu_buffer);
  4697. goto again;
  4698. case RINGBUF_TYPE_DATA:
  4699. if (ts && !(*ts)) {
  4700. *ts = cpu_buffer->read_stamp + event->time_delta;
  4701. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4702. cpu_buffer->cpu, ts);
  4703. }
  4704. if (lost_events)
  4705. *lost_events = rb_lost_events(cpu_buffer);
  4706. return event;
  4707. default:
  4708. RB_WARN_ON(cpu_buffer, 1);
  4709. }
  4710. return NULL;
  4711. }
  4712. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  4713. static struct ring_buffer_event *
  4714. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  4715. {
  4716. struct trace_buffer *buffer;
  4717. struct ring_buffer_per_cpu *cpu_buffer;
  4718. struct ring_buffer_event *event;
  4719. int nr_loops = 0;
  4720. if (ts)
  4721. *ts = 0;
  4722. cpu_buffer = iter->cpu_buffer;
  4723. buffer = cpu_buffer->buffer;
  4724. /*
  4725. * Check if someone performed a consuming read to the buffer
  4726. * or removed some pages from the buffer. In these cases,
  4727. * iterator was invalidated and we need to reset it.
  4728. */
  4729. if (unlikely(iter->cache_read != cpu_buffer->read ||
  4730. iter->cache_reader_page != cpu_buffer->reader_page ||
  4731. iter->cache_pages_removed != cpu_buffer->pages_removed))
  4732. rb_iter_reset(iter);
  4733. again:
  4734. if (ring_buffer_iter_empty(iter))
  4735. return NULL;
  4736. /*
  4737. * As the writer can mess with what the iterator is trying
  4738. * to read, just give up if we fail to get an event after
  4739. * three tries. The iterator is not as reliable when reading
  4740. * the ring buffer with an active write as the consumer is.
  4741. * Do not warn if the three failures is reached.
  4742. */
  4743. if (++nr_loops > 3)
  4744. return NULL;
  4745. if (rb_per_cpu_empty(cpu_buffer))
  4746. return NULL;
  4747. if (iter->head >= rb_page_size(iter->head_page)) {
  4748. rb_inc_iter(iter);
  4749. goto again;
  4750. }
  4751. event = rb_iter_head_event(iter);
  4752. if (!event)
  4753. goto again;
  4754. switch (event->type_len) {
  4755. case RINGBUF_TYPE_PADDING:
  4756. if (rb_null_event(event)) {
  4757. rb_inc_iter(iter);
  4758. goto again;
  4759. }
  4760. rb_advance_iter(iter);
  4761. return event;
  4762. case RINGBUF_TYPE_TIME_EXTEND:
  4763. /* Internal data, OK to advance */
  4764. rb_advance_iter(iter);
  4765. goto again;
  4766. case RINGBUF_TYPE_TIME_STAMP:
  4767. if (ts) {
  4768. *ts = rb_event_time_stamp(event);
  4769. *ts = rb_fix_abs_ts(*ts, iter->head_page->page->time_stamp);
  4770. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  4771. cpu_buffer->cpu, ts);
  4772. }
  4773. /* Internal data, OK to advance */
  4774. rb_advance_iter(iter);
  4775. goto again;
  4776. case RINGBUF_TYPE_DATA:
  4777. if (ts && !(*ts)) {
  4778. *ts = iter->read_stamp + event->time_delta;
  4779. ring_buffer_normalize_time_stamp(buffer,
  4780. cpu_buffer->cpu, ts);
  4781. }
  4782. return event;
  4783. default:
  4784. RB_WARN_ON(cpu_buffer, 1);
  4785. }
  4786. return NULL;
  4787. }
  4788. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  4789. static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
  4790. {
  4791. if (likely(!in_nmi())) {
  4792. raw_spin_lock(&cpu_buffer->reader_lock);
  4793. return true;
  4794. }
  4795. /*
  4796. * If an NMI die dumps out the content of the ring buffer
  4797. * trylock must be used to prevent a deadlock if the NMI
  4798. * preempted a task that holds the ring buffer locks. If
  4799. * we get the lock then all is fine, if not, then continue
  4800. * to do the read, but this can corrupt the ring buffer,
  4801. * so it must be permanently disabled from future writes.
  4802. * Reading from NMI is a oneshot deal.
  4803. */
  4804. if (raw_spin_trylock(&cpu_buffer->reader_lock))
  4805. return true;
  4806. /* Continue without locking, but disable the ring buffer */
  4807. atomic_inc(&cpu_buffer->record_disabled);
  4808. return false;
  4809. }
  4810. static inline void
  4811. rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
  4812. {
  4813. if (likely(locked))
  4814. raw_spin_unlock(&cpu_buffer->reader_lock);
  4815. }
  4816. /**
  4817. * ring_buffer_peek - peek at the next event to be read
  4818. * @buffer: The ring buffer to read
  4819. * @cpu: The cpu to peak at
  4820. * @ts: The timestamp counter of this event.
  4821. * @lost_events: a variable to store if events were lost (may be NULL)
  4822. *
  4823. * This will return the event that will be read next, but does
  4824. * not consume the data.
  4825. */
  4826. struct ring_buffer_event *
  4827. ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
  4828. unsigned long *lost_events)
  4829. {
  4830. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  4831. struct ring_buffer_event *event;
  4832. unsigned long flags;
  4833. bool dolock;
  4834. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4835. return NULL;
  4836. again:
  4837. local_irq_save(flags);
  4838. dolock = rb_reader_lock(cpu_buffer);
  4839. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  4840. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4841. rb_advance_reader(cpu_buffer);
  4842. rb_reader_unlock(cpu_buffer, dolock);
  4843. local_irq_restore(flags);
  4844. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4845. goto again;
  4846. return event;
  4847. }
  4848. /** ring_buffer_iter_dropped - report if there are dropped events
  4849. * @iter: The ring buffer iterator
  4850. *
  4851. * Returns true if there was dropped events since the last peek.
  4852. */
  4853. bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
  4854. {
  4855. bool ret = iter->missed_events != 0;
  4856. iter->missed_events = 0;
  4857. return ret;
  4858. }
  4859. EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
  4860. /**
  4861. * ring_buffer_iter_peek - peek at the next event to be read
  4862. * @iter: The ring buffer iterator
  4863. * @ts: The timestamp counter of this event.
  4864. *
  4865. * This will return the event that will be read next, but does
  4866. * not increment the iterator.
  4867. */
  4868. struct ring_buffer_event *
  4869. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  4870. {
  4871. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  4872. struct ring_buffer_event *event;
  4873. unsigned long flags;
  4874. again:
  4875. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4876. event = rb_iter_peek(iter, ts);
  4877. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  4878. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4879. goto again;
  4880. return event;
  4881. }
  4882. /**
  4883. * ring_buffer_consume - return an event and consume it
  4884. * @buffer: The ring buffer to get the next event from
  4885. * @cpu: the cpu to read the buffer from
  4886. * @ts: a variable to store the timestamp (may be NULL)
  4887. * @lost_events: a variable to store if events were lost (may be NULL)
  4888. *
  4889. * Returns the next event in the ring buffer, and that event is consumed.
  4890. * Meaning, that sequential reads will keep returning a different event,
  4891. * and eventually empty the ring buffer if the producer is slower.
  4892. */
  4893. struct ring_buffer_event *
  4894. ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
  4895. unsigned long *lost_events)
  4896. {
  4897. struct ring_buffer_per_cpu *cpu_buffer;
  4898. struct ring_buffer_event *event = NULL;
  4899. unsigned long flags;
  4900. bool dolock;
  4901. again:
  4902. /* might be called in atomic */
  4903. preempt_disable();
  4904. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4905. goto out;
  4906. cpu_buffer = buffer->buffers[cpu];
  4907. local_irq_save(flags);
  4908. dolock = rb_reader_lock(cpu_buffer);
  4909. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  4910. if (event) {
  4911. cpu_buffer->lost_events = 0;
  4912. rb_advance_reader(cpu_buffer);
  4913. }
  4914. rb_reader_unlock(cpu_buffer, dolock);
  4915. local_irq_restore(flags);
  4916. out:
  4917. preempt_enable();
  4918. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  4919. goto again;
  4920. return event;
  4921. }
  4922. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  4923. /**
  4924. * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
  4925. * @buffer: The ring buffer to read from
  4926. * @cpu: The cpu buffer to iterate over
  4927. * @flags: gfp flags to use for memory allocation
  4928. *
  4929. * This performs the initial preparations necessary to iterate
  4930. * through the buffer. Memory is allocated, buffer resizing
  4931. * is disabled, and the iterator pointer is returned to the caller.
  4932. *
  4933. * After a sequence of ring_buffer_read_prepare calls, the user is
  4934. * expected to make at least one call to ring_buffer_read_prepare_sync.
  4935. * Afterwards, ring_buffer_read_start is invoked to get things going
  4936. * for real.
  4937. *
  4938. * This overall must be paired with ring_buffer_read_finish.
  4939. */
  4940. struct ring_buffer_iter *
  4941. ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
  4942. {
  4943. struct ring_buffer_per_cpu *cpu_buffer;
  4944. struct ring_buffer_iter *iter;
  4945. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  4946. return NULL;
  4947. iter = kzalloc(sizeof(*iter), flags);
  4948. if (!iter)
  4949. return NULL;
  4950. /* Holds the entire event: data and meta data */
  4951. iter->event_size = buffer->subbuf_size;
  4952. iter->event = kmalloc(iter->event_size, flags);
  4953. if (!iter->event) {
  4954. kfree(iter);
  4955. return NULL;
  4956. }
  4957. cpu_buffer = buffer->buffers[cpu];
  4958. iter->cpu_buffer = cpu_buffer;
  4959. atomic_inc(&cpu_buffer->resize_disabled);
  4960. return iter;
  4961. }
  4962. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
  4963. /**
  4964. * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
  4965. *
  4966. * All previously invoked ring_buffer_read_prepare calls to prepare
  4967. * iterators will be synchronized. Afterwards, read_buffer_read_start
  4968. * calls on those iterators are allowed.
  4969. */
  4970. void
  4971. ring_buffer_read_prepare_sync(void)
  4972. {
  4973. synchronize_rcu();
  4974. }
  4975. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
  4976. /**
  4977. * ring_buffer_read_start - start a non consuming read of the buffer
  4978. * @iter: The iterator returned by ring_buffer_read_prepare
  4979. *
  4980. * This finalizes the startup of an iteration through the buffer.
  4981. * The iterator comes from a call to ring_buffer_read_prepare and
  4982. * an intervening ring_buffer_read_prepare_sync must have been
  4983. * performed.
  4984. *
  4985. * Must be paired with ring_buffer_read_finish.
  4986. */
  4987. void
  4988. ring_buffer_read_start(struct ring_buffer_iter *iter)
  4989. {
  4990. struct ring_buffer_per_cpu *cpu_buffer;
  4991. unsigned long flags;
  4992. if (!iter)
  4993. return;
  4994. cpu_buffer = iter->cpu_buffer;
  4995. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  4996. arch_spin_lock(&cpu_buffer->lock);
  4997. rb_iter_reset(iter);
  4998. arch_spin_unlock(&cpu_buffer->lock);
  4999. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5000. }
  5001. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  5002. /**
  5003. * ring_buffer_read_finish - finish reading the iterator of the buffer
  5004. * @iter: The iterator retrieved by ring_buffer_start
  5005. *
  5006. * This re-enables resizing of the buffer, and frees the iterator.
  5007. */
  5008. void
  5009. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  5010. {
  5011. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  5012. /* Use this opportunity to check the integrity of the ring buffer. */
  5013. rb_check_pages(cpu_buffer);
  5014. atomic_dec(&cpu_buffer->resize_disabled);
  5015. kfree(iter->event);
  5016. kfree(iter);
  5017. }
  5018. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  5019. /**
  5020. * ring_buffer_iter_advance - advance the iterator to the next location
  5021. * @iter: The ring buffer iterator
  5022. *
  5023. * Move the location of the iterator such that the next read will
  5024. * be the next location of the iterator.
  5025. */
  5026. void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
  5027. {
  5028. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  5029. unsigned long flags;
  5030. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5031. rb_advance_iter(iter);
  5032. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5033. }
  5034. EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
  5035. /**
  5036. * ring_buffer_size - return the size of the ring buffer (in bytes)
  5037. * @buffer: The ring buffer.
  5038. * @cpu: The CPU to get ring buffer size from.
  5039. */
  5040. unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
  5041. {
  5042. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5043. return 0;
  5044. return buffer->subbuf_size * buffer->buffers[cpu]->nr_pages;
  5045. }
  5046. EXPORT_SYMBOL_GPL(ring_buffer_size);
  5047. /**
  5048. * ring_buffer_max_event_size - return the max data size of an event
  5049. * @buffer: The ring buffer.
  5050. *
  5051. * Returns the maximum size an event can be.
  5052. */
  5053. unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer)
  5054. {
  5055. /* If abs timestamp is requested, events have a timestamp too */
  5056. if (ring_buffer_time_stamp_abs(buffer))
  5057. return buffer->max_data_size - RB_LEN_TIME_EXTEND;
  5058. return buffer->max_data_size;
  5059. }
  5060. EXPORT_SYMBOL_GPL(ring_buffer_max_event_size);
  5061. static void rb_clear_buffer_page(struct buffer_page *page)
  5062. {
  5063. local_set(&page->write, 0);
  5064. local_set(&page->entries, 0);
  5065. rb_init_page(page->page);
  5066. page->read = 0;
  5067. }
  5068. static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5069. {
  5070. struct trace_buffer_meta *meta = cpu_buffer->meta_page;
  5071. if (!meta)
  5072. return;
  5073. meta->reader.read = cpu_buffer->reader_page->read;
  5074. meta->reader.id = cpu_buffer->reader_page->id;
  5075. meta->reader.lost_events = cpu_buffer->lost_events;
  5076. meta->entries = local_read(&cpu_buffer->entries);
  5077. meta->overrun = local_read(&cpu_buffer->overrun);
  5078. meta->read = cpu_buffer->read;
  5079. /* Some archs do not have data cache coherency between kernel and user-space */
  5080. flush_dcache_folio(virt_to_folio(cpu_buffer->meta_page));
  5081. }
  5082. static void
  5083. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  5084. {
  5085. struct buffer_page *page;
  5086. rb_head_page_deactivate(cpu_buffer);
  5087. cpu_buffer->head_page
  5088. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  5089. rb_clear_buffer_page(cpu_buffer->head_page);
  5090. list_for_each_entry(page, cpu_buffer->pages, list) {
  5091. rb_clear_buffer_page(page);
  5092. }
  5093. cpu_buffer->tail_page = cpu_buffer->head_page;
  5094. cpu_buffer->commit_page = cpu_buffer->head_page;
  5095. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  5096. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  5097. rb_clear_buffer_page(cpu_buffer->reader_page);
  5098. local_set(&cpu_buffer->entries_bytes, 0);
  5099. local_set(&cpu_buffer->overrun, 0);
  5100. local_set(&cpu_buffer->commit_overrun, 0);
  5101. local_set(&cpu_buffer->dropped_events, 0);
  5102. local_set(&cpu_buffer->entries, 0);
  5103. local_set(&cpu_buffer->committing, 0);
  5104. local_set(&cpu_buffer->commits, 0);
  5105. local_set(&cpu_buffer->pages_touched, 0);
  5106. local_set(&cpu_buffer->pages_lost, 0);
  5107. local_set(&cpu_buffer->pages_read, 0);
  5108. cpu_buffer->last_pages_touch = 0;
  5109. cpu_buffer->shortest_full = 0;
  5110. cpu_buffer->read = 0;
  5111. cpu_buffer->read_bytes = 0;
  5112. rb_time_set(&cpu_buffer->write_stamp, 0);
  5113. rb_time_set(&cpu_buffer->before_stamp, 0);
  5114. memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp));
  5115. cpu_buffer->lost_events = 0;
  5116. cpu_buffer->last_overrun = 0;
  5117. rb_head_page_activate(cpu_buffer);
  5118. cpu_buffer->pages_removed = 0;
  5119. if (cpu_buffer->mapped) {
  5120. rb_update_meta_page(cpu_buffer);
  5121. if (cpu_buffer->ring_meta) {
  5122. struct ring_buffer_meta *meta = cpu_buffer->ring_meta;
  5123. meta->commit_buffer = meta->head_buffer;
  5124. }
  5125. }
  5126. }
  5127. /* Must have disabled the cpu buffer then done a synchronize_rcu */
  5128. static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  5129. {
  5130. unsigned long flags;
  5131. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5132. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  5133. goto out;
  5134. arch_spin_lock(&cpu_buffer->lock);
  5135. rb_reset_cpu(cpu_buffer);
  5136. arch_spin_unlock(&cpu_buffer->lock);
  5137. out:
  5138. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5139. }
  5140. /**
  5141. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  5142. * @buffer: The ring buffer to reset a per cpu buffer of
  5143. * @cpu: The CPU buffer to be reset
  5144. */
  5145. void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
  5146. {
  5147. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  5148. struct ring_buffer_meta *meta;
  5149. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5150. return;
  5151. /* prevent another thread from changing buffer sizes */
  5152. mutex_lock(&buffer->mutex);
  5153. atomic_inc(&cpu_buffer->resize_disabled);
  5154. atomic_inc(&cpu_buffer->record_disabled);
  5155. /* Make sure all commits have finished */
  5156. synchronize_rcu();
  5157. reset_disabled_cpu_buffer(cpu_buffer);
  5158. atomic_dec(&cpu_buffer->record_disabled);
  5159. atomic_dec(&cpu_buffer->resize_disabled);
  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. mutex_unlock(&buffer->mutex);
  5165. }
  5166. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  5167. /* Flag to ensure proper resetting of atomic variables */
  5168. #define RESET_BIT (1 << 30)
  5169. /**
  5170. * ring_buffer_reset_online_cpus - reset a ring buffer per CPU buffer
  5171. * @buffer: The ring buffer to reset a per cpu buffer of
  5172. */
  5173. void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
  5174. {
  5175. struct ring_buffer_per_cpu *cpu_buffer;
  5176. struct ring_buffer_meta *meta;
  5177. int cpu;
  5178. /* prevent another thread from changing buffer sizes */
  5179. mutex_lock(&buffer->mutex);
  5180. for_each_online_buffer_cpu(buffer, cpu) {
  5181. cpu_buffer = buffer->buffers[cpu];
  5182. atomic_add(RESET_BIT, &cpu_buffer->resize_disabled);
  5183. atomic_inc(&cpu_buffer->record_disabled);
  5184. }
  5185. /* Make sure all commits have finished */
  5186. synchronize_rcu();
  5187. for_each_buffer_cpu(buffer, cpu) {
  5188. cpu_buffer = buffer->buffers[cpu];
  5189. /*
  5190. * If a CPU came online during the synchronize_rcu(), then
  5191. * ignore it.
  5192. */
  5193. if (!(atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT))
  5194. continue;
  5195. reset_disabled_cpu_buffer(cpu_buffer);
  5196. /* Make sure persistent meta now uses this buffer's addresses */
  5197. meta = rb_range_meta(buffer, 0, cpu_buffer->cpu);
  5198. if (meta)
  5199. rb_meta_init_text_addr(meta);
  5200. atomic_dec(&cpu_buffer->record_disabled);
  5201. atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled);
  5202. }
  5203. mutex_unlock(&buffer->mutex);
  5204. }
  5205. /**
  5206. * ring_buffer_reset - reset a ring buffer
  5207. * @buffer: The ring buffer to reset all cpu buffers
  5208. */
  5209. void ring_buffer_reset(struct trace_buffer *buffer)
  5210. {
  5211. struct ring_buffer_per_cpu *cpu_buffer;
  5212. int cpu;
  5213. /* prevent another thread from changing buffer sizes */
  5214. mutex_lock(&buffer->mutex);
  5215. for_each_buffer_cpu(buffer, cpu) {
  5216. cpu_buffer = buffer->buffers[cpu];
  5217. atomic_inc(&cpu_buffer->resize_disabled);
  5218. atomic_inc(&cpu_buffer->record_disabled);
  5219. }
  5220. /* Make sure all commits have finished */
  5221. synchronize_rcu();
  5222. for_each_buffer_cpu(buffer, cpu) {
  5223. cpu_buffer = buffer->buffers[cpu];
  5224. reset_disabled_cpu_buffer(cpu_buffer);
  5225. atomic_dec(&cpu_buffer->record_disabled);
  5226. atomic_dec(&cpu_buffer->resize_disabled);
  5227. }
  5228. mutex_unlock(&buffer->mutex);
  5229. }
  5230. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  5231. /**
  5232. * ring_buffer_empty - is the ring buffer empty?
  5233. * @buffer: The ring buffer to test
  5234. */
  5235. bool ring_buffer_empty(struct trace_buffer *buffer)
  5236. {
  5237. struct ring_buffer_per_cpu *cpu_buffer;
  5238. unsigned long flags;
  5239. bool dolock;
  5240. bool ret;
  5241. int cpu;
  5242. /* yes this is racy, but if you don't like the race, lock the buffer */
  5243. for_each_buffer_cpu(buffer, cpu) {
  5244. cpu_buffer = buffer->buffers[cpu];
  5245. local_irq_save(flags);
  5246. dolock = rb_reader_lock(cpu_buffer);
  5247. ret = rb_per_cpu_empty(cpu_buffer);
  5248. rb_reader_unlock(cpu_buffer, dolock);
  5249. local_irq_restore(flags);
  5250. if (!ret)
  5251. return false;
  5252. }
  5253. return true;
  5254. }
  5255. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  5256. /**
  5257. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  5258. * @buffer: The ring buffer
  5259. * @cpu: The CPU buffer to test
  5260. */
  5261. bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
  5262. {
  5263. struct ring_buffer_per_cpu *cpu_buffer;
  5264. unsigned long flags;
  5265. bool dolock;
  5266. bool ret;
  5267. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5268. return true;
  5269. cpu_buffer = buffer->buffers[cpu];
  5270. local_irq_save(flags);
  5271. dolock = rb_reader_lock(cpu_buffer);
  5272. ret = rb_per_cpu_empty(cpu_buffer);
  5273. rb_reader_unlock(cpu_buffer, dolock);
  5274. local_irq_restore(flags);
  5275. return ret;
  5276. }
  5277. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  5278. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  5279. /**
  5280. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  5281. * @buffer_a: One buffer to swap with
  5282. * @buffer_b: The other buffer to swap with
  5283. * @cpu: the CPU of the buffers to swap
  5284. *
  5285. * This function is useful for tracers that want to take a "snapshot"
  5286. * of a CPU buffer and has another back up buffer lying around.
  5287. * it is expected that the tracer handles the cpu buffer not being
  5288. * used at the moment.
  5289. */
  5290. int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
  5291. struct trace_buffer *buffer_b, int cpu)
  5292. {
  5293. struct ring_buffer_per_cpu *cpu_buffer_a;
  5294. struct ring_buffer_per_cpu *cpu_buffer_b;
  5295. int ret = -EINVAL;
  5296. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  5297. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  5298. goto out;
  5299. cpu_buffer_a = buffer_a->buffers[cpu];
  5300. cpu_buffer_b = buffer_b->buffers[cpu];
  5301. /* It's up to the callers to not try to swap mapped buffers */
  5302. if (WARN_ON_ONCE(cpu_buffer_a->mapped || cpu_buffer_b->mapped)) {
  5303. ret = -EBUSY;
  5304. goto out;
  5305. }
  5306. /* At least make sure the two buffers are somewhat the same */
  5307. if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
  5308. goto out;
  5309. if (buffer_a->subbuf_order != buffer_b->subbuf_order)
  5310. goto out;
  5311. ret = -EAGAIN;
  5312. if (atomic_read(&buffer_a->record_disabled))
  5313. goto out;
  5314. if (atomic_read(&buffer_b->record_disabled))
  5315. goto out;
  5316. if (atomic_read(&cpu_buffer_a->record_disabled))
  5317. goto out;
  5318. if (atomic_read(&cpu_buffer_b->record_disabled))
  5319. goto out;
  5320. /*
  5321. * We can't do a synchronize_rcu here because this
  5322. * function can be called in atomic context.
  5323. * Normally this will be called from the same CPU as cpu.
  5324. * If not it's up to the caller to protect this.
  5325. */
  5326. atomic_inc(&cpu_buffer_a->record_disabled);
  5327. atomic_inc(&cpu_buffer_b->record_disabled);
  5328. ret = -EBUSY;
  5329. if (local_read(&cpu_buffer_a->committing))
  5330. goto out_dec;
  5331. if (local_read(&cpu_buffer_b->committing))
  5332. goto out_dec;
  5333. /*
  5334. * When resize is in progress, we cannot swap it because
  5335. * it will mess the state of the cpu buffer.
  5336. */
  5337. if (atomic_read(&buffer_a->resizing))
  5338. goto out_dec;
  5339. if (atomic_read(&buffer_b->resizing))
  5340. goto out_dec;
  5341. buffer_a->buffers[cpu] = cpu_buffer_b;
  5342. buffer_b->buffers[cpu] = cpu_buffer_a;
  5343. cpu_buffer_b->buffer = buffer_a;
  5344. cpu_buffer_a->buffer = buffer_b;
  5345. ret = 0;
  5346. out_dec:
  5347. atomic_dec(&cpu_buffer_a->record_disabled);
  5348. atomic_dec(&cpu_buffer_b->record_disabled);
  5349. out:
  5350. return ret;
  5351. }
  5352. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  5353. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  5354. /**
  5355. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  5356. * @buffer: the buffer to allocate for.
  5357. * @cpu: the cpu buffer to allocate.
  5358. *
  5359. * This function is used in conjunction with ring_buffer_read_page.
  5360. * When reading a full page from the ring buffer, these functions
  5361. * can be used to speed up the process. The calling function should
  5362. * allocate a few pages first with this function. Then when it
  5363. * needs to get pages from the ring buffer, it passes the result
  5364. * of this function into ring_buffer_read_page, which will swap
  5365. * the page that was allocated, with the read page of the buffer.
  5366. *
  5367. * Returns:
  5368. * The page allocated, or ERR_PTR
  5369. */
  5370. struct buffer_data_read_page *
  5371. ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
  5372. {
  5373. struct ring_buffer_per_cpu *cpu_buffer;
  5374. struct buffer_data_read_page *bpage = NULL;
  5375. unsigned long flags;
  5376. struct page *page;
  5377. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5378. return ERR_PTR(-ENODEV);
  5379. bpage = kzalloc(sizeof(*bpage), GFP_KERNEL);
  5380. if (!bpage)
  5381. return ERR_PTR(-ENOMEM);
  5382. bpage->order = buffer->subbuf_order;
  5383. cpu_buffer = buffer->buffers[cpu];
  5384. local_irq_save(flags);
  5385. arch_spin_lock(&cpu_buffer->lock);
  5386. if (cpu_buffer->free_page) {
  5387. bpage->data = cpu_buffer->free_page;
  5388. cpu_buffer->free_page = NULL;
  5389. }
  5390. arch_spin_unlock(&cpu_buffer->lock);
  5391. local_irq_restore(flags);
  5392. if (bpage->data)
  5393. goto out;
  5394. page = alloc_pages_node(cpu_to_node(cpu),
  5395. GFP_KERNEL | __GFP_NORETRY | __GFP_COMP | __GFP_ZERO,
  5396. cpu_buffer->buffer->subbuf_order);
  5397. if (!page) {
  5398. kfree(bpage);
  5399. return ERR_PTR(-ENOMEM);
  5400. }
  5401. bpage->data = page_address(page);
  5402. out:
  5403. rb_init_page(bpage->data);
  5404. return bpage;
  5405. }
  5406. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  5407. /**
  5408. * ring_buffer_free_read_page - free an allocated read page
  5409. * @buffer: the buffer the page was allocate for
  5410. * @cpu: the cpu buffer the page came from
  5411. * @data_page: the page to free
  5412. *
  5413. * Free a page allocated from ring_buffer_alloc_read_page.
  5414. */
  5415. void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu,
  5416. struct buffer_data_read_page *data_page)
  5417. {
  5418. struct ring_buffer_per_cpu *cpu_buffer;
  5419. struct buffer_data_page *bpage = data_page->data;
  5420. struct page *page = virt_to_page(bpage);
  5421. unsigned long flags;
  5422. if (!buffer || !buffer->buffers || !buffer->buffers[cpu])
  5423. return;
  5424. cpu_buffer = buffer->buffers[cpu];
  5425. /*
  5426. * If the page is still in use someplace else, or order of the page
  5427. * is different from the subbuffer order of the buffer -
  5428. * we can't reuse it
  5429. */
  5430. if (page_ref_count(page) > 1 || data_page->order != buffer->subbuf_order)
  5431. goto out;
  5432. local_irq_save(flags);
  5433. arch_spin_lock(&cpu_buffer->lock);
  5434. if (!cpu_buffer->free_page) {
  5435. cpu_buffer->free_page = bpage;
  5436. bpage = NULL;
  5437. }
  5438. arch_spin_unlock(&cpu_buffer->lock);
  5439. local_irq_restore(flags);
  5440. out:
  5441. free_pages((unsigned long)bpage, data_page->order);
  5442. kfree(data_page);
  5443. }
  5444. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  5445. /**
  5446. * ring_buffer_read_page - extract a page from the ring buffer
  5447. * @buffer: buffer to extract from
  5448. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  5449. * @len: amount to extract
  5450. * @cpu: the cpu of the buffer to extract
  5451. * @full: should the extraction only happen when the page is full.
  5452. *
  5453. * This function will pull out a page from the ring buffer and consume it.
  5454. * @data_page must be the address of the variable that was returned
  5455. * from ring_buffer_alloc_read_page. This is because the page might be used
  5456. * to swap with a page in the ring buffer.
  5457. *
  5458. * for example:
  5459. * rpage = ring_buffer_alloc_read_page(buffer, cpu);
  5460. * if (IS_ERR(rpage))
  5461. * return PTR_ERR(rpage);
  5462. * ret = ring_buffer_read_page(buffer, rpage, len, cpu, 0);
  5463. * if (ret >= 0)
  5464. * process_page(ring_buffer_read_page_data(rpage), ret);
  5465. * ring_buffer_free_read_page(buffer, cpu, rpage);
  5466. *
  5467. * When @full is set, the function will not return true unless
  5468. * the writer is off the reader page.
  5469. *
  5470. * Note: it is up to the calling functions to handle sleeps and wakeups.
  5471. * The ring buffer can be used anywhere in the kernel and can not
  5472. * blindly call wake_up. The layer that uses the ring buffer must be
  5473. * responsible for that.
  5474. *
  5475. * Returns:
  5476. * >=0 if data has been transferred, returns the offset of consumed data.
  5477. * <0 if no data has been transferred.
  5478. */
  5479. int ring_buffer_read_page(struct trace_buffer *buffer,
  5480. struct buffer_data_read_page *data_page,
  5481. size_t len, int cpu, int full)
  5482. {
  5483. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  5484. struct ring_buffer_event *event;
  5485. struct buffer_data_page *bpage;
  5486. struct buffer_page *reader;
  5487. unsigned long missed_events;
  5488. unsigned long flags;
  5489. unsigned int commit;
  5490. unsigned int read;
  5491. u64 save_timestamp;
  5492. int ret = -1;
  5493. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5494. goto out;
  5495. /*
  5496. * If len is not big enough to hold the page header, then
  5497. * we can not copy anything.
  5498. */
  5499. if (len <= BUF_PAGE_HDR_SIZE)
  5500. goto out;
  5501. len -= BUF_PAGE_HDR_SIZE;
  5502. if (!data_page || !data_page->data)
  5503. goto out;
  5504. if (data_page->order != buffer->subbuf_order)
  5505. goto out;
  5506. bpage = data_page->data;
  5507. if (!bpage)
  5508. goto out;
  5509. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5510. reader = rb_get_reader_page(cpu_buffer);
  5511. if (!reader)
  5512. goto out_unlock;
  5513. event = rb_reader_event(cpu_buffer);
  5514. read = reader->read;
  5515. commit = rb_page_size(reader);
  5516. /* Check if any events were dropped */
  5517. missed_events = cpu_buffer->lost_events;
  5518. /*
  5519. * If this page has been partially read or
  5520. * if len is not big enough to read the rest of the page or
  5521. * a writer is still on the page, then
  5522. * we must copy the data from the page to the buffer.
  5523. * Otherwise, we can simply swap the page with the one passed in.
  5524. */
  5525. if (read || (len < (commit - read)) ||
  5526. cpu_buffer->reader_page == cpu_buffer->commit_page ||
  5527. cpu_buffer->mapped) {
  5528. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  5529. unsigned int rpos = read;
  5530. unsigned int pos = 0;
  5531. unsigned int size;
  5532. /*
  5533. * If a full page is expected, this can still be returned
  5534. * if there's been a previous partial read and the
  5535. * rest of the page can be read and the commit page is off
  5536. * the reader page.
  5537. */
  5538. if (full &&
  5539. (!read || (len < (commit - read)) ||
  5540. cpu_buffer->reader_page == cpu_buffer->commit_page))
  5541. goto out_unlock;
  5542. if (len > (commit - read))
  5543. len = (commit - read);
  5544. /* Always keep the time extend and data together */
  5545. size = rb_event_ts_length(event);
  5546. if (len < size)
  5547. goto out_unlock;
  5548. /* save the current timestamp, since the user will need it */
  5549. save_timestamp = cpu_buffer->read_stamp;
  5550. /* Need to copy one event at a time */
  5551. do {
  5552. /* We need the size of one event, because
  5553. * rb_advance_reader only advances by one event,
  5554. * whereas rb_event_ts_length may include the size of
  5555. * one or two events.
  5556. * We have already ensured there's enough space if this
  5557. * is a time extend. */
  5558. size = rb_event_length(event);
  5559. memcpy(bpage->data + pos, rpage->data + rpos, size);
  5560. len -= size;
  5561. rb_advance_reader(cpu_buffer);
  5562. rpos = reader->read;
  5563. pos += size;
  5564. if (rpos >= commit)
  5565. break;
  5566. event = rb_reader_event(cpu_buffer);
  5567. /* Always keep the time extend and data together */
  5568. size = rb_event_ts_length(event);
  5569. } while (len >= size);
  5570. /* update bpage */
  5571. local_set(&bpage->commit, pos);
  5572. bpage->time_stamp = save_timestamp;
  5573. /* we copied everything to the beginning */
  5574. read = 0;
  5575. } else {
  5576. /* update the entry counter */
  5577. cpu_buffer->read += rb_page_entries(reader);
  5578. cpu_buffer->read_bytes += rb_page_size(reader);
  5579. /* swap the pages */
  5580. rb_init_page(bpage);
  5581. bpage = reader->page;
  5582. reader->page = data_page->data;
  5583. local_set(&reader->write, 0);
  5584. local_set(&reader->entries, 0);
  5585. reader->read = 0;
  5586. data_page->data = bpage;
  5587. /*
  5588. * Use the real_end for the data size,
  5589. * This gives us a chance to store the lost events
  5590. * on the page.
  5591. */
  5592. if (reader->real_end)
  5593. local_set(&bpage->commit, reader->real_end);
  5594. }
  5595. ret = read;
  5596. cpu_buffer->lost_events = 0;
  5597. commit = local_read(&bpage->commit);
  5598. /*
  5599. * Set a flag in the commit field if we lost events
  5600. */
  5601. if (missed_events) {
  5602. /* If there is room at the end of the page to save the
  5603. * missed events, then record it there.
  5604. */
  5605. if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
  5606. memcpy(&bpage->data[commit], &missed_events,
  5607. sizeof(missed_events));
  5608. local_add(RB_MISSED_STORED, &bpage->commit);
  5609. commit += sizeof(missed_events);
  5610. }
  5611. local_add(RB_MISSED_EVENTS, &bpage->commit);
  5612. }
  5613. /*
  5614. * This page may be off to user land. Zero it out here.
  5615. */
  5616. if (commit < buffer->subbuf_size)
  5617. memset(&bpage->data[commit], 0, buffer->subbuf_size - commit);
  5618. out_unlock:
  5619. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5620. out:
  5621. return ret;
  5622. }
  5623. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  5624. /**
  5625. * ring_buffer_read_page_data - get pointer to the data in the page.
  5626. * @page: the page to get the data from
  5627. *
  5628. * Returns pointer to the actual data in this page.
  5629. */
  5630. void *ring_buffer_read_page_data(struct buffer_data_read_page *page)
  5631. {
  5632. return page->data;
  5633. }
  5634. EXPORT_SYMBOL_GPL(ring_buffer_read_page_data);
  5635. /**
  5636. * ring_buffer_subbuf_size_get - get size of the sub buffer.
  5637. * @buffer: the buffer to get the sub buffer size from
  5638. *
  5639. * Returns size of the sub buffer, in bytes.
  5640. */
  5641. int ring_buffer_subbuf_size_get(struct trace_buffer *buffer)
  5642. {
  5643. return buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
  5644. }
  5645. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_size_get);
  5646. /**
  5647. * ring_buffer_subbuf_order_get - get order of system sub pages in one buffer page.
  5648. * @buffer: The ring_buffer to get the system sub page order from
  5649. *
  5650. * By default, one ring buffer sub page equals to one system page. This parameter
  5651. * is configurable, per ring buffer. The size of the ring buffer sub page can be
  5652. * extended, but must be an order of system page size.
  5653. *
  5654. * Returns the order of buffer sub page size, in system pages:
  5655. * 0 means the sub buffer size is 1 system page and so forth.
  5656. * In case of an error < 0 is returned.
  5657. */
  5658. int ring_buffer_subbuf_order_get(struct trace_buffer *buffer)
  5659. {
  5660. if (!buffer)
  5661. return -EINVAL;
  5662. return buffer->subbuf_order;
  5663. }
  5664. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_get);
  5665. /**
  5666. * ring_buffer_subbuf_order_set - set the size of ring buffer sub page.
  5667. * @buffer: The ring_buffer to set the new page size.
  5668. * @order: Order of the system pages in one sub buffer page
  5669. *
  5670. * By default, one ring buffer pages equals to one system page. This API can be
  5671. * used to set new size of the ring buffer page. The size must be order of
  5672. * system page size, that's why the input parameter @order is the order of
  5673. * system pages that are allocated for one ring buffer page:
  5674. * 0 - 1 system page
  5675. * 1 - 2 system pages
  5676. * 3 - 4 system pages
  5677. * ...
  5678. *
  5679. * Returns 0 on success or < 0 in case of an error.
  5680. */
  5681. int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
  5682. {
  5683. struct ring_buffer_per_cpu *cpu_buffer;
  5684. struct buffer_page *bpage, *tmp;
  5685. int old_order, old_size;
  5686. int nr_pages;
  5687. int psize;
  5688. int err;
  5689. int cpu;
  5690. if (!buffer || order < 0)
  5691. return -EINVAL;
  5692. if (buffer->subbuf_order == order)
  5693. return 0;
  5694. psize = (1 << order) * PAGE_SIZE;
  5695. if (psize <= BUF_PAGE_HDR_SIZE)
  5696. return -EINVAL;
  5697. /* Size of a subbuf cannot be greater than the write counter */
  5698. if (psize > RB_WRITE_MASK + 1)
  5699. return -EINVAL;
  5700. old_order = buffer->subbuf_order;
  5701. old_size = buffer->subbuf_size;
  5702. /* prevent another thread from changing buffer sizes */
  5703. mutex_lock(&buffer->mutex);
  5704. atomic_inc(&buffer->record_disabled);
  5705. /* Make sure all commits have finished */
  5706. synchronize_rcu();
  5707. buffer->subbuf_order = order;
  5708. buffer->subbuf_size = psize - BUF_PAGE_HDR_SIZE;
  5709. /* Make sure all new buffers are allocated, before deleting the old ones */
  5710. for_each_buffer_cpu(buffer, cpu) {
  5711. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5712. continue;
  5713. cpu_buffer = buffer->buffers[cpu];
  5714. if (cpu_buffer->mapped) {
  5715. err = -EBUSY;
  5716. goto error;
  5717. }
  5718. /* Update the number of pages to match the new size */
  5719. nr_pages = old_size * buffer->buffers[cpu]->nr_pages;
  5720. nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size);
  5721. /* we need a minimum of two pages */
  5722. if (nr_pages < 2)
  5723. nr_pages = 2;
  5724. cpu_buffer->nr_pages_to_update = nr_pages;
  5725. /* Include the reader page */
  5726. nr_pages++;
  5727. /* Allocate the new size buffer */
  5728. INIT_LIST_HEAD(&cpu_buffer->new_pages);
  5729. if (__rb_allocate_pages(cpu_buffer, nr_pages,
  5730. &cpu_buffer->new_pages)) {
  5731. /* not enough memory for new pages */
  5732. err = -ENOMEM;
  5733. goto error;
  5734. }
  5735. }
  5736. for_each_buffer_cpu(buffer, cpu) {
  5737. struct buffer_data_page *old_free_data_page;
  5738. struct list_head old_pages;
  5739. unsigned long flags;
  5740. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5741. continue;
  5742. cpu_buffer = buffer->buffers[cpu];
  5743. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5744. /* Clear the head bit to make the link list normal to read */
  5745. rb_head_page_deactivate(cpu_buffer);
  5746. /*
  5747. * Collect buffers from the cpu_buffer pages list and the
  5748. * reader_page on old_pages, so they can be freed later when not
  5749. * under a spinlock. The pages list is a linked list with no
  5750. * head, adding old_pages turns it into a regular list with
  5751. * old_pages being the head.
  5752. */
  5753. list_add(&old_pages, cpu_buffer->pages);
  5754. list_add(&cpu_buffer->reader_page->list, &old_pages);
  5755. /* One page was allocated for the reader page */
  5756. cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next,
  5757. struct buffer_page, list);
  5758. list_del_init(&cpu_buffer->reader_page->list);
  5759. /* Install the new pages, remove the head from the list */
  5760. cpu_buffer->pages = cpu_buffer->new_pages.next;
  5761. list_del_init(&cpu_buffer->new_pages);
  5762. cpu_buffer->cnt++;
  5763. cpu_buffer->head_page
  5764. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  5765. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  5766. cpu_buffer->nr_pages = cpu_buffer->nr_pages_to_update;
  5767. cpu_buffer->nr_pages_to_update = 0;
  5768. old_free_data_page = cpu_buffer->free_page;
  5769. cpu_buffer->free_page = NULL;
  5770. rb_head_page_activate(cpu_buffer);
  5771. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5772. /* Free old sub buffers */
  5773. list_for_each_entry_safe(bpage, tmp, &old_pages, list) {
  5774. list_del_init(&bpage->list);
  5775. free_buffer_page(bpage);
  5776. }
  5777. free_pages((unsigned long)old_free_data_page, old_order);
  5778. rb_check_pages(cpu_buffer);
  5779. }
  5780. atomic_dec(&buffer->record_disabled);
  5781. mutex_unlock(&buffer->mutex);
  5782. return 0;
  5783. error:
  5784. buffer->subbuf_order = old_order;
  5785. buffer->subbuf_size = old_size;
  5786. atomic_dec(&buffer->record_disabled);
  5787. mutex_unlock(&buffer->mutex);
  5788. for_each_buffer_cpu(buffer, cpu) {
  5789. cpu_buffer = buffer->buffers[cpu];
  5790. if (!cpu_buffer->nr_pages_to_update)
  5791. continue;
  5792. list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, list) {
  5793. list_del_init(&bpage->list);
  5794. free_buffer_page(bpage);
  5795. }
  5796. }
  5797. return err;
  5798. }
  5799. EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set);
  5800. static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5801. {
  5802. struct page *page;
  5803. if (cpu_buffer->meta_page)
  5804. return 0;
  5805. page = alloc_page(GFP_USER | __GFP_ZERO);
  5806. if (!page)
  5807. return -ENOMEM;
  5808. cpu_buffer->meta_page = page_to_virt(page);
  5809. return 0;
  5810. }
  5811. static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer)
  5812. {
  5813. unsigned long addr = (unsigned long)cpu_buffer->meta_page;
  5814. free_page(addr);
  5815. cpu_buffer->meta_page = NULL;
  5816. }
  5817. static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer,
  5818. unsigned long *subbuf_ids)
  5819. {
  5820. struct trace_buffer_meta *meta = cpu_buffer->meta_page;
  5821. unsigned int nr_subbufs = cpu_buffer->nr_pages + 1;
  5822. struct buffer_page *first_subbuf, *subbuf;
  5823. int id = 0;
  5824. subbuf_ids[id] = (unsigned long)cpu_buffer->reader_page->page;
  5825. cpu_buffer->reader_page->id = id++;
  5826. first_subbuf = subbuf = rb_set_head_page(cpu_buffer);
  5827. do {
  5828. if (WARN_ON(id >= nr_subbufs))
  5829. break;
  5830. subbuf_ids[id] = (unsigned long)subbuf->page;
  5831. subbuf->id = id;
  5832. rb_inc_page(&subbuf);
  5833. id++;
  5834. } while (subbuf != first_subbuf);
  5835. /* install subbuf ID to kern VA translation */
  5836. cpu_buffer->subbuf_ids = subbuf_ids;
  5837. meta->meta_struct_len = sizeof(*meta);
  5838. meta->nr_subbufs = nr_subbufs;
  5839. meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE;
  5840. meta->meta_page_size = meta->subbuf_size;
  5841. rb_update_meta_page(cpu_buffer);
  5842. }
  5843. static struct ring_buffer_per_cpu *
  5844. rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu)
  5845. {
  5846. struct ring_buffer_per_cpu *cpu_buffer;
  5847. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5848. return ERR_PTR(-EINVAL);
  5849. cpu_buffer = buffer->buffers[cpu];
  5850. mutex_lock(&cpu_buffer->mapping_lock);
  5851. if (!cpu_buffer->user_mapped) {
  5852. mutex_unlock(&cpu_buffer->mapping_lock);
  5853. return ERR_PTR(-ENODEV);
  5854. }
  5855. return cpu_buffer;
  5856. }
  5857. static void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  5858. {
  5859. mutex_unlock(&cpu_buffer->mapping_lock);
  5860. }
  5861. /*
  5862. * Fast-path for rb_buffer_(un)map(). Called whenever the meta-page doesn't need
  5863. * to be set-up or torn-down.
  5864. */
  5865. static int __rb_inc_dec_mapped(struct ring_buffer_per_cpu *cpu_buffer,
  5866. bool inc)
  5867. {
  5868. unsigned long flags;
  5869. lockdep_assert_held(&cpu_buffer->mapping_lock);
  5870. /* mapped is always greater or equal to user_mapped */
  5871. if (WARN_ON(cpu_buffer->mapped < cpu_buffer->user_mapped))
  5872. return -EINVAL;
  5873. if (inc && cpu_buffer->mapped == UINT_MAX)
  5874. return -EBUSY;
  5875. if (WARN_ON(!inc && cpu_buffer->user_mapped == 0))
  5876. return -EINVAL;
  5877. mutex_lock(&cpu_buffer->buffer->mutex);
  5878. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  5879. if (inc) {
  5880. cpu_buffer->user_mapped++;
  5881. cpu_buffer->mapped++;
  5882. } else {
  5883. cpu_buffer->user_mapped--;
  5884. cpu_buffer->mapped--;
  5885. }
  5886. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  5887. mutex_unlock(&cpu_buffer->buffer->mutex);
  5888. return 0;
  5889. }
  5890. /*
  5891. * +--------------+ pgoff == 0
  5892. * | meta page |
  5893. * +--------------+ pgoff == 1
  5894. * | subbuffer 0 |
  5895. * | |
  5896. * +--------------+ pgoff == (1 + (1 << subbuf_order))
  5897. * | subbuffer 1 |
  5898. * | |
  5899. * ...
  5900. */
  5901. #ifdef CONFIG_MMU
  5902. static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
  5903. struct vm_area_struct *vma)
  5904. {
  5905. unsigned long nr_subbufs, nr_pages, nr_vma_pages, pgoff = vma->vm_pgoff;
  5906. unsigned int subbuf_pages, subbuf_order;
  5907. struct page **pages;
  5908. int p = 0, s = 0;
  5909. int err;
  5910. /* Refuse MP_PRIVATE or writable mappings */
  5911. if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC ||
  5912. !(vma->vm_flags & VM_MAYSHARE))
  5913. return -EPERM;
  5914. subbuf_order = cpu_buffer->buffer->subbuf_order;
  5915. subbuf_pages = 1 << subbuf_order;
  5916. if (subbuf_order && pgoff % subbuf_pages)
  5917. return -EINVAL;
  5918. /*
  5919. * Make sure the mapping cannot become writable later. Also tell the VM
  5920. * to not touch these pages (VM_DONTCOPY | VM_DONTEXPAND).
  5921. */
  5922. vm_flags_mod(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP,
  5923. VM_MAYWRITE);
  5924. lockdep_assert_held(&cpu_buffer->mapping_lock);
  5925. nr_subbufs = cpu_buffer->nr_pages + 1; /* + reader-subbuf */
  5926. nr_pages = ((nr_subbufs + 1) << subbuf_order); /* + meta-page */
  5927. if (nr_pages <= pgoff)
  5928. return -EINVAL;
  5929. nr_pages -= pgoff;
  5930. nr_vma_pages = vma_pages(vma);
  5931. if (!nr_vma_pages || nr_vma_pages > nr_pages)
  5932. return -EINVAL;
  5933. nr_pages = nr_vma_pages;
  5934. pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
  5935. if (!pages)
  5936. return -ENOMEM;
  5937. if (!pgoff) {
  5938. unsigned long meta_page_padding;
  5939. pages[p++] = virt_to_page(cpu_buffer->meta_page);
  5940. /*
  5941. * Pad with the zero-page to align the meta-page with the
  5942. * sub-buffers.
  5943. */
  5944. meta_page_padding = subbuf_pages - 1;
  5945. while (meta_page_padding-- && p < nr_pages) {
  5946. unsigned long __maybe_unused zero_addr =
  5947. vma->vm_start + (PAGE_SIZE * p);
  5948. pages[p++] = ZERO_PAGE(zero_addr);
  5949. }
  5950. } else {
  5951. /* Skip the meta-page */
  5952. pgoff -= subbuf_pages;
  5953. s += pgoff / subbuf_pages;
  5954. }
  5955. while (p < nr_pages) {
  5956. struct page *page;
  5957. int off = 0;
  5958. if (WARN_ON_ONCE(s >= nr_subbufs)) {
  5959. err = -EINVAL;
  5960. goto out;
  5961. }
  5962. page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
  5963. for (; off < (1 << (subbuf_order)); off++, page++) {
  5964. if (p >= nr_pages)
  5965. break;
  5966. pages[p++] = page;
  5967. }
  5968. s++;
  5969. }
  5970. err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
  5971. out:
  5972. kfree(pages);
  5973. return err;
  5974. }
  5975. #else
  5976. static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
  5977. struct vm_area_struct *vma)
  5978. {
  5979. return -EOPNOTSUPP;
  5980. }
  5981. #endif
  5982. int ring_buffer_map(struct trace_buffer *buffer, int cpu,
  5983. struct vm_area_struct *vma)
  5984. {
  5985. struct ring_buffer_per_cpu *cpu_buffer;
  5986. unsigned long flags, *subbuf_ids;
  5987. int err = 0;
  5988. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  5989. return -EINVAL;
  5990. cpu_buffer = buffer->buffers[cpu];
  5991. mutex_lock(&cpu_buffer->mapping_lock);
  5992. if (cpu_buffer->user_mapped) {
  5993. err = __rb_map_vma(cpu_buffer, vma);
  5994. if (!err)
  5995. err = __rb_inc_dec_mapped(cpu_buffer, true);
  5996. mutex_unlock(&cpu_buffer->mapping_lock);
  5997. return err;
  5998. }
  5999. /* prevent another thread from changing buffer/sub-buffer sizes */
  6000. mutex_lock(&buffer->mutex);
  6001. err = rb_alloc_meta_page(cpu_buffer);
  6002. if (err)
  6003. goto unlock;
  6004. /* subbuf_ids include the reader while nr_pages does not */
  6005. subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1, sizeof(*subbuf_ids), GFP_KERNEL);
  6006. if (!subbuf_ids) {
  6007. rb_free_meta_page(cpu_buffer);
  6008. err = -ENOMEM;
  6009. goto unlock;
  6010. }
  6011. atomic_inc(&cpu_buffer->resize_disabled);
  6012. /*
  6013. * Lock all readers to block any subbuf swap until the subbuf IDs are
  6014. * assigned.
  6015. */
  6016. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6017. rb_setup_ids_meta_page(cpu_buffer, subbuf_ids);
  6018. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6019. err = __rb_map_vma(cpu_buffer, vma);
  6020. if (!err) {
  6021. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6022. /* This is the first time it is mapped by user */
  6023. cpu_buffer->mapped++;
  6024. cpu_buffer->user_mapped = 1;
  6025. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6026. } else {
  6027. kfree(cpu_buffer->subbuf_ids);
  6028. cpu_buffer->subbuf_ids = NULL;
  6029. rb_free_meta_page(cpu_buffer);
  6030. atomic_dec(&cpu_buffer->resize_disabled);
  6031. }
  6032. unlock:
  6033. mutex_unlock(&buffer->mutex);
  6034. mutex_unlock(&cpu_buffer->mapping_lock);
  6035. return err;
  6036. }
  6037. int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
  6038. {
  6039. struct ring_buffer_per_cpu *cpu_buffer;
  6040. unsigned long flags;
  6041. int err = 0;
  6042. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  6043. return -EINVAL;
  6044. cpu_buffer = buffer->buffers[cpu];
  6045. mutex_lock(&cpu_buffer->mapping_lock);
  6046. if (!cpu_buffer->user_mapped) {
  6047. err = -ENODEV;
  6048. goto out;
  6049. } else if (cpu_buffer->user_mapped > 1) {
  6050. __rb_inc_dec_mapped(cpu_buffer, false);
  6051. goto out;
  6052. }
  6053. mutex_lock(&buffer->mutex);
  6054. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6055. /* This is the last user space mapping */
  6056. if (!WARN_ON_ONCE(cpu_buffer->mapped < cpu_buffer->user_mapped))
  6057. cpu_buffer->mapped--;
  6058. cpu_buffer->user_mapped = 0;
  6059. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6060. kfree(cpu_buffer->subbuf_ids);
  6061. cpu_buffer->subbuf_ids = NULL;
  6062. rb_free_meta_page(cpu_buffer);
  6063. atomic_dec(&cpu_buffer->resize_disabled);
  6064. mutex_unlock(&buffer->mutex);
  6065. out:
  6066. mutex_unlock(&cpu_buffer->mapping_lock);
  6067. return err;
  6068. }
  6069. int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu)
  6070. {
  6071. struct ring_buffer_per_cpu *cpu_buffer;
  6072. struct buffer_page *reader;
  6073. unsigned long missed_events;
  6074. unsigned long reader_size;
  6075. unsigned long flags;
  6076. cpu_buffer = rb_get_mapped_buffer(buffer, cpu);
  6077. if (IS_ERR(cpu_buffer))
  6078. return (int)PTR_ERR(cpu_buffer);
  6079. raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  6080. consume:
  6081. if (rb_per_cpu_empty(cpu_buffer))
  6082. goto out;
  6083. reader_size = rb_page_size(cpu_buffer->reader_page);
  6084. /*
  6085. * There are data to be read on the current reader page, we can
  6086. * return to the caller. But before that, we assume the latter will read
  6087. * everything. Let's update the kernel reader accordingly.
  6088. */
  6089. if (cpu_buffer->reader_page->read < reader_size) {
  6090. while (cpu_buffer->reader_page->read < reader_size)
  6091. rb_advance_reader(cpu_buffer);
  6092. goto out;
  6093. }
  6094. reader = rb_get_reader_page(cpu_buffer);
  6095. if (WARN_ON(!reader))
  6096. goto out;
  6097. /* Check if any events were dropped */
  6098. missed_events = cpu_buffer->lost_events;
  6099. if (cpu_buffer->reader_page != cpu_buffer->commit_page) {
  6100. if (missed_events) {
  6101. struct buffer_data_page *bpage = reader->page;
  6102. unsigned int commit;
  6103. /*
  6104. * Use the real_end for the data size,
  6105. * This gives us a chance to store the lost events
  6106. * on the page.
  6107. */
  6108. if (reader->real_end)
  6109. local_set(&bpage->commit, reader->real_end);
  6110. /*
  6111. * If there is room at the end of the page to save the
  6112. * missed events, then record it there.
  6113. */
  6114. commit = rb_page_size(reader);
  6115. if (buffer->subbuf_size - commit >= sizeof(missed_events)) {
  6116. memcpy(&bpage->data[commit], &missed_events,
  6117. sizeof(missed_events));
  6118. local_add(RB_MISSED_STORED, &bpage->commit);
  6119. }
  6120. local_add(RB_MISSED_EVENTS, &bpage->commit);
  6121. }
  6122. } else {
  6123. /*
  6124. * There really shouldn't be any missed events if the commit
  6125. * is on the reader page.
  6126. */
  6127. WARN_ON_ONCE(missed_events);
  6128. }
  6129. cpu_buffer->lost_events = 0;
  6130. goto consume;
  6131. out:
  6132. /* Some archs do not have data cache coherency between kernel and user-space */
  6133. flush_dcache_folio(virt_to_folio(cpu_buffer->reader_page->page));
  6134. rb_update_meta_page(cpu_buffer);
  6135. raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  6136. rb_put_mapped_buffer(cpu_buffer);
  6137. return 0;
  6138. }
  6139. /*
  6140. * We only allocate new buffers, never free them if the CPU goes down.
  6141. * If we were to free the buffer, then the user would lose any trace that was in
  6142. * the buffer.
  6143. */
  6144. int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
  6145. {
  6146. struct trace_buffer *buffer;
  6147. long nr_pages_same;
  6148. int cpu_i;
  6149. unsigned long nr_pages;
  6150. buffer = container_of(node, struct trace_buffer, node);
  6151. if (cpumask_test_cpu(cpu, buffer->cpumask))
  6152. return 0;
  6153. nr_pages = 0;
  6154. nr_pages_same = 1;
  6155. /* check if all cpu sizes are same */
  6156. for_each_buffer_cpu(buffer, cpu_i) {
  6157. /* fill in the size from first enabled cpu */
  6158. if (nr_pages == 0)
  6159. nr_pages = buffer->buffers[cpu_i]->nr_pages;
  6160. if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
  6161. nr_pages_same = 0;
  6162. break;
  6163. }
  6164. }
  6165. /* allocate minimum pages, user can later expand it */
  6166. if (!nr_pages_same)
  6167. nr_pages = 2;
  6168. buffer->buffers[cpu] =
  6169. rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
  6170. if (!buffer->buffers[cpu]) {
  6171. WARN(1, "failed to allocate ring buffer on CPU %u\n",
  6172. cpu);
  6173. return -ENOMEM;
  6174. }
  6175. smp_wmb();
  6176. cpumask_set_cpu(cpu, buffer->cpumask);
  6177. return 0;
  6178. }
  6179. #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
  6180. /*
  6181. * This is a basic integrity check of the ring buffer.
  6182. * Late in the boot cycle this test will run when configured in.
  6183. * It will kick off a thread per CPU that will go into a loop
  6184. * writing to the per cpu ring buffer various sizes of data.
  6185. * Some of the data will be large items, some small.
  6186. *
  6187. * Another thread is created that goes into a spin, sending out
  6188. * IPIs to the other CPUs to also write into the ring buffer.
  6189. * this is to test the nesting ability of the buffer.
  6190. *
  6191. * Basic stats are recorded and reported. If something in the
  6192. * ring buffer should happen that's not expected, a big warning
  6193. * is displayed and all ring buffers are disabled.
  6194. */
  6195. static struct task_struct *rb_threads[NR_CPUS] __initdata;
  6196. struct rb_test_data {
  6197. struct trace_buffer *buffer;
  6198. unsigned long events;
  6199. unsigned long bytes_written;
  6200. unsigned long bytes_alloc;
  6201. unsigned long bytes_dropped;
  6202. unsigned long events_nested;
  6203. unsigned long bytes_written_nested;
  6204. unsigned long bytes_alloc_nested;
  6205. unsigned long bytes_dropped_nested;
  6206. int min_size_nested;
  6207. int max_size_nested;
  6208. int max_size;
  6209. int min_size;
  6210. int cpu;
  6211. int cnt;
  6212. };
  6213. static struct rb_test_data rb_data[NR_CPUS] __initdata;
  6214. /* 1 meg per cpu */
  6215. #define RB_TEST_BUFFER_SIZE 1048576
  6216. static char rb_string[] __initdata =
  6217. "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
  6218. "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
  6219. "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
  6220. static bool rb_test_started __initdata;
  6221. struct rb_item {
  6222. int size;
  6223. char str[];
  6224. };
  6225. static __init int rb_write_something(struct rb_test_data *data, bool nested)
  6226. {
  6227. struct ring_buffer_event *event;
  6228. struct rb_item *item;
  6229. bool started;
  6230. int event_len;
  6231. int size;
  6232. int len;
  6233. int cnt;
  6234. /* Have nested writes different that what is written */
  6235. cnt = data->cnt + (nested ? 27 : 0);
  6236. /* Multiply cnt by ~e, to make some unique increment */
  6237. size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
  6238. len = size + sizeof(struct rb_item);
  6239. started = rb_test_started;
  6240. /* read rb_test_started before checking buffer enabled */
  6241. smp_rmb();
  6242. event = ring_buffer_lock_reserve(data->buffer, len);
  6243. if (!event) {
  6244. /* Ignore dropped events before test starts. */
  6245. if (started) {
  6246. if (nested)
  6247. data->bytes_dropped += len;
  6248. else
  6249. data->bytes_dropped_nested += len;
  6250. }
  6251. return len;
  6252. }
  6253. event_len = ring_buffer_event_length(event);
  6254. if (RB_WARN_ON(data->buffer, event_len < len))
  6255. goto out;
  6256. item = ring_buffer_event_data(event);
  6257. item->size = size;
  6258. memcpy(item->str, rb_string, size);
  6259. if (nested) {
  6260. data->bytes_alloc_nested += event_len;
  6261. data->bytes_written_nested += len;
  6262. data->events_nested++;
  6263. if (!data->min_size_nested || len < data->min_size_nested)
  6264. data->min_size_nested = len;
  6265. if (len > data->max_size_nested)
  6266. data->max_size_nested = len;
  6267. } else {
  6268. data->bytes_alloc += event_len;
  6269. data->bytes_written += len;
  6270. data->events++;
  6271. if (!data->min_size || len < data->min_size)
  6272. data->max_size = len;
  6273. if (len > data->max_size)
  6274. data->max_size = len;
  6275. }
  6276. out:
  6277. ring_buffer_unlock_commit(data->buffer);
  6278. return 0;
  6279. }
  6280. static __init int rb_test(void *arg)
  6281. {
  6282. struct rb_test_data *data = arg;
  6283. while (!kthread_should_stop()) {
  6284. rb_write_something(data, false);
  6285. data->cnt++;
  6286. set_current_state(TASK_INTERRUPTIBLE);
  6287. /* Now sleep between a min of 100-300us and a max of 1ms */
  6288. usleep_range(((data->cnt % 3) + 1) * 100, 1000);
  6289. }
  6290. return 0;
  6291. }
  6292. static __init void rb_ipi(void *ignore)
  6293. {
  6294. struct rb_test_data *data;
  6295. int cpu = smp_processor_id();
  6296. data = &rb_data[cpu];
  6297. rb_write_something(data, true);
  6298. }
  6299. static __init int rb_hammer_test(void *arg)
  6300. {
  6301. while (!kthread_should_stop()) {
  6302. /* Send an IPI to all cpus to write data! */
  6303. smp_call_function(rb_ipi, NULL, 1);
  6304. /* No sleep, but for non preempt, let others run */
  6305. schedule();
  6306. }
  6307. return 0;
  6308. }
  6309. static __init int test_ringbuffer(void)
  6310. {
  6311. struct task_struct *rb_hammer;
  6312. struct trace_buffer *buffer;
  6313. int cpu;
  6314. int ret = 0;
  6315. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  6316. pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
  6317. return 0;
  6318. }
  6319. pr_info("Running ring buffer tests...\n");
  6320. buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
  6321. if (WARN_ON(!buffer))
  6322. return 0;
  6323. /* Disable buffer so that threads can't write to it yet */
  6324. ring_buffer_record_off(buffer);
  6325. for_each_online_cpu(cpu) {
  6326. rb_data[cpu].buffer = buffer;
  6327. rb_data[cpu].cpu = cpu;
  6328. rb_data[cpu].cnt = cpu;
  6329. rb_threads[cpu] = kthread_run_on_cpu(rb_test, &rb_data[cpu],
  6330. cpu, "rbtester/%u");
  6331. if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
  6332. pr_cont("FAILED\n");
  6333. ret = PTR_ERR(rb_threads[cpu]);
  6334. goto out_free;
  6335. }
  6336. }
  6337. /* Now create the rb hammer! */
  6338. rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
  6339. if (WARN_ON(IS_ERR(rb_hammer))) {
  6340. pr_cont("FAILED\n");
  6341. ret = PTR_ERR(rb_hammer);
  6342. goto out_free;
  6343. }
  6344. ring_buffer_record_on(buffer);
  6345. /*
  6346. * Show buffer is enabled before setting rb_test_started.
  6347. * Yes there's a small race window where events could be
  6348. * dropped and the thread wont catch it. But when a ring
  6349. * buffer gets enabled, there will always be some kind of
  6350. * delay before other CPUs see it. Thus, we don't care about
  6351. * those dropped events. We care about events dropped after
  6352. * the threads see that the buffer is active.
  6353. */
  6354. smp_wmb();
  6355. rb_test_started = true;
  6356. set_current_state(TASK_INTERRUPTIBLE);
  6357. /* Just run for 10 seconds */;
  6358. schedule_timeout(10 * HZ);
  6359. kthread_stop(rb_hammer);
  6360. out_free:
  6361. for_each_online_cpu(cpu) {
  6362. if (!rb_threads[cpu])
  6363. break;
  6364. kthread_stop(rb_threads[cpu]);
  6365. }
  6366. if (ret) {
  6367. ring_buffer_free(buffer);
  6368. return ret;
  6369. }
  6370. /* Report! */
  6371. pr_info("finished\n");
  6372. for_each_online_cpu(cpu) {
  6373. struct ring_buffer_event *event;
  6374. struct rb_test_data *data = &rb_data[cpu];
  6375. struct rb_item *item;
  6376. unsigned long total_events;
  6377. unsigned long total_dropped;
  6378. unsigned long total_written;
  6379. unsigned long total_alloc;
  6380. unsigned long total_read = 0;
  6381. unsigned long total_size = 0;
  6382. unsigned long total_len = 0;
  6383. unsigned long total_lost = 0;
  6384. unsigned long lost;
  6385. int big_event_size;
  6386. int small_event_size;
  6387. ret = -1;
  6388. total_events = data->events + data->events_nested;
  6389. total_written = data->bytes_written + data->bytes_written_nested;
  6390. total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
  6391. total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
  6392. big_event_size = data->max_size + data->max_size_nested;
  6393. small_event_size = data->min_size + data->min_size_nested;
  6394. pr_info("CPU %d:\n", cpu);
  6395. pr_info(" events: %ld\n", total_events);
  6396. pr_info(" dropped bytes: %ld\n", total_dropped);
  6397. pr_info(" alloced bytes: %ld\n", total_alloc);
  6398. pr_info(" written bytes: %ld\n", total_written);
  6399. pr_info(" biggest event: %d\n", big_event_size);
  6400. pr_info(" smallest event: %d\n", small_event_size);
  6401. if (RB_WARN_ON(buffer, total_dropped))
  6402. break;
  6403. ret = 0;
  6404. while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
  6405. total_lost += lost;
  6406. item = ring_buffer_event_data(event);
  6407. total_len += ring_buffer_event_length(event);
  6408. total_size += item->size + sizeof(struct rb_item);
  6409. if (memcmp(&item->str[0], rb_string, item->size) != 0) {
  6410. pr_info("FAILED!\n");
  6411. pr_info("buffer had: %.*s\n", item->size, item->str);
  6412. pr_info("expected: %.*s\n", item->size, rb_string);
  6413. RB_WARN_ON(buffer, 1);
  6414. ret = -1;
  6415. break;
  6416. }
  6417. total_read++;
  6418. }
  6419. if (ret)
  6420. break;
  6421. ret = -1;
  6422. pr_info(" read events: %ld\n", total_read);
  6423. pr_info(" lost events: %ld\n", total_lost);
  6424. pr_info(" total events: %ld\n", total_lost + total_read);
  6425. pr_info(" recorded len bytes: %ld\n", total_len);
  6426. pr_info(" recorded size bytes: %ld\n", total_size);
  6427. if (total_lost) {
  6428. pr_info(" With dropped events, record len and size may not match\n"
  6429. " alloced and written from above\n");
  6430. } else {
  6431. if (RB_WARN_ON(buffer, total_len != total_alloc ||
  6432. total_size != total_written))
  6433. break;
  6434. }
  6435. if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
  6436. break;
  6437. ret = 0;
  6438. }
  6439. if (!ret)
  6440. pr_info("Ring buffer PASSED!\n");
  6441. ring_buffer_free(buffer);
  6442. return 0;
  6443. }
  6444. late_initcall(test_ringbuffer);
  6445. #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */