ring_buffer.c 129 KB

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