ring_buffer.c 154 KB

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