ring_buffer.c 129 KB

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