ring_buffer.c 172 KB

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