ring_buffer.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899
  1. /*
  2. * Generic ring buffer
  3. *
  4. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  5. */
  6. #include <linux/ring_buffer.h>
  7. #include <linux/trace_clock.h>
  8. #include <linux/ftrace_irq.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/module.h>
  14. #include <linux/percpu.h>
  15. #include <linux/mutex.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/list.h>
  19. #include <linux/cpu.h>
  20. #include <linux/fs.h>
  21. #include "trace.h"
  22. /*
  23. * The ring buffer is made up of a list of pages. A separate list of pages is
  24. * allocated for each CPU. A writer may only write to a buffer that is
  25. * associated with the CPU it is currently executing on. A reader may read
  26. * from any per cpu buffer.
  27. *
  28. * The reader is special. For each per cpu buffer, the reader has its own
  29. * reader page. When a reader has read the entire reader page, this reader
  30. * page is swapped with another page in the ring buffer.
  31. *
  32. * Now, as long as the writer is off the reader page, the reader can do what
  33. * ever it wants with that page. The writer will never write to that page
  34. * again (as long as it is out of the ring buffer).
  35. *
  36. * Here's some silly ASCII art.
  37. *
  38. * +------+
  39. * |reader| RING BUFFER
  40. * |page |
  41. * +------+ +---+ +---+ +---+
  42. * | |-->| |-->| |
  43. * +---+ +---+ +---+
  44. * ^ |
  45. * | |
  46. * +---------------+
  47. *
  48. *
  49. * +------+
  50. * |reader| RING BUFFER
  51. * |page |------------------v
  52. * +------+ +---+ +---+ +---+
  53. * | |-->| |-->| |
  54. * +---+ +---+ +---+
  55. * ^ |
  56. * | |
  57. * +---------------+
  58. *
  59. *
  60. * +------+
  61. * |reader| RING BUFFER
  62. * |page |------------------v
  63. * +------+ +---+ +---+ +---+
  64. * ^ | |-->| |-->| |
  65. * | +---+ +---+ +---+
  66. * | |
  67. * | |
  68. * +------------------------------+
  69. *
  70. *
  71. * +------+
  72. * |buffer| RING BUFFER
  73. * |page |------------------v
  74. * +------+ +---+ +---+ +---+
  75. * ^ | | | |-->| |
  76. * | New +---+ +---+ +---+
  77. * | Reader------^ |
  78. * | page |
  79. * +------------------------------+
  80. *
  81. *
  82. * After we make this swap, the reader can hand this page off to the splice
  83. * code and be done with it. It can even allocate a new page if it needs to
  84. * and swap that into the ring buffer.
  85. *
  86. * We will be using cmpxchg soon to make all this lockless.
  87. *
  88. */
  89. /*
  90. * A fast way to enable or disable all ring buffers is to
  91. * call tracing_on or tracing_off. Turning off the ring buffers
  92. * prevents all ring buffers from being recorded to.
  93. * Turning this switch on, makes it OK to write to the
  94. * ring buffer, if the ring buffer is enabled itself.
  95. *
  96. * There's three layers that must be on in order to write
  97. * to the ring buffer.
  98. *
  99. * 1) This global flag must be set.
  100. * 2) The ring buffer must be enabled for recording.
  101. * 3) The per cpu buffer must be enabled for recording.
  102. *
  103. * In case of an anomaly, this global flag has a bit set that
  104. * will permantly disable all ring buffers.
  105. */
  106. /*
  107. * Global flag to disable all recording to ring buffers
  108. * This has two bits: ON, DISABLED
  109. *
  110. * ON DISABLED
  111. * ---- ----------
  112. * 0 0 : ring buffers are off
  113. * 1 0 : ring buffers are on
  114. * X 1 : ring buffers are permanently disabled
  115. */
  116. enum {
  117. RB_BUFFERS_ON_BIT = 0,
  118. RB_BUFFERS_DISABLED_BIT = 1,
  119. };
  120. enum {
  121. RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
  122. RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
  123. };
  124. static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
  125. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  126. /**
  127. * tracing_on - enable all tracing buffers
  128. *
  129. * This function enables all tracing buffers that may have been
  130. * disabled with tracing_off.
  131. */
  132. void tracing_on(void)
  133. {
  134. set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  135. }
  136. EXPORT_SYMBOL_GPL(tracing_on);
  137. /**
  138. * tracing_off - turn off all tracing buffers
  139. *
  140. * This function stops all tracing buffers from recording data.
  141. * It does not disable any overhead the tracers themselves may
  142. * be causing. This function simply causes all recording to
  143. * the ring buffers to fail.
  144. */
  145. void tracing_off(void)
  146. {
  147. clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  148. }
  149. EXPORT_SYMBOL_GPL(tracing_off);
  150. /**
  151. * tracing_off_permanent - permanently disable ring buffers
  152. *
  153. * This function, once called, will disable all ring buffers
  154. * permanently.
  155. */
  156. void tracing_off_permanent(void)
  157. {
  158. set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
  159. }
  160. /**
  161. * tracing_is_on - show state of ring buffers enabled
  162. */
  163. int tracing_is_on(void)
  164. {
  165. return ring_buffer_flags == RB_BUFFERS_ON;
  166. }
  167. EXPORT_SYMBOL_GPL(tracing_is_on);
  168. #include "trace.h"
  169. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  170. #define RB_ALIGNMENT 4U
  171. #define RB_MAX_SMALL_DATA 28
  172. enum {
  173. RB_LEN_TIME_EXTEND = 8,
  174. RB_LEN_TIME_STAMP = 16,
  175. };
  176. static inline int rb_null_event(struct ring_buffer_event *event)
  177. {
  178. return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
  179. }
  180. static inline int rb_discarded_event(struct ring_buffer_event *event)
  181. {
  182. return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
  183. }
  184. static void rb_event_set_padding(struct ring_buffer_event *event)
  185. {
  186. event->type = RINGBUF_TYPE_PADDING;
  187. event->time_delta = 0;
  188. }
  189. /**
  190. * ring_buffer_event_discard - discard an event in the ring buffer
  191. * @buffer: the ring buffer
  192. * @event: the event to discard
  193. *
  194. * Sometimes a event that is in the ring buffer needs to be ignored.
  195. * This function lets the user discard an event in the ring buffer
  196. * and then that event will not be read later.
  197. *
  198. * Note, it is up to the user to be careful with this, and protect
  199. * against races. If the user discards an event that has been consumed
  200. * it is possible that it could corrupt the ring buffer.
  201. */
  202. void ring_buffer_event_discard(struct ring_buffer_event *event)
  203. {
  204. event->type = RINGBUF_TYPE_PADDING;
  205. /* time delta must be non zero */
  206. if (!event->time_delta)
  207. event->time_delta = 1;
  208. }
  209. static unsigned
  210. rb_event_data_length(struct ring_buffer_event *event)
  211. {
  212. unsigned length;
  213. if (event->len)
  214. length = event->len * RB_ALIGNMENT;
  215. else
  216. length = event->array[0];
  217. return length + RB_EVNT_HDR_SIZE;
  218. }
  219. /* inline for ring buffer fast paths */
  220. static unsigned
  221. rb_event_length(struct ring_buffer_event *event)
  222. {
  223. switch (event->type) {
  224. case RINGBUF_TYPE_PADDING:
  225. if (rb_null_event(event))
  226. /* undefined */
  227. return -1;
  228. return rb_event_data_length(event);
  229. case RINGBUF_TYPE_TIME_EXTEND:
  230. return RB_LEN_TIME_EXTEND;
  231. case RINGBUF_TYPE_TIME_STAMP:
  232. return RB_LEN_TIME_STAMP;
  233. case RINGBUF_TYPE_DATA:
  234. return rb_event_data_length(event);
  235. default:
  236. BUG();
  237. }
  238. /* not hit */
  239. return 0;
  240. }
  241. /**
  242. * ring_buffer_event_length - return the length of the event
  243. * @event: the event to get the length of
  244. */
  245. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  246. {
  247. unsigned length = rb_event_length(event);
  248. if (event->type != RINGBUF_TYPE_DATA)
  249. return length;
  250. length -= RB_EVNT_HDR_SIZE;
  251. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  252. length -= sizeof(event->array[0]);
  253. return length;
  254. }
  255. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  256. /* inline for ring buffer fast paths */
  257. static void *
  258. rb_event_data(struct ring_buffer_event *event)
  259. {
  260. BUG_ON(event->type != RINGBUF_TYPE_DATA);
  261. /* If length is in len field, then array[0] has the data */
  262. if (event->len)
  263. return (void *)&event->array[0];
  264. /* Otherwise length is in array[0] and array[1] has the data */
  265. return (void *)&event->array[1];
  266. }
  267. /**
  268. * ring_buffer_event_data - return the data of the event
  269. * @event: the event to get the data from
  270. */
  271. void *ring_buffer_event_data(struct ring_buffer_event *event)
  272. {
  273. return rb_event_data(event);
  274. }
  275. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  276. #define for_each_buffer_cpu(buffer, cpu) \
  277. for_each_cpu(cpu, buffer->cpumask)
  278. #define TS_SHIFT 27
  279. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  280. #define TS_DELTA_TEST (~TS_MASK)
  281. struct buffer_data_page {
  282. u64 time_stamp; /* page time stamp */
  283. local_t commit; /* write committed index */
  284. unsigned char data[]; /* data of buffer page */
  285. };
  286. struct buffer_page {
  287. local_t write; /* index for next write */
  288. unsigned read; /* index for next read */
  289. struct list_head list; /* list of free pages */
  290. struct buffer_data_page *page; /* Actual data page */
  291. };
  292. static void rb_init_page(struct buffer_data_page *bpage)
  293. {
  294. local_set(&bpage->commit, 0);
  295. }
  296. /**
  297. * ring_buffer_page_len - the size of data on the page.
  298. * @page: The page to read
  299. *
  300. * Returns the amount of data on the page, including buffer page header.
  301. */
  302. size_t ring_buffer_page_len(void *page)
  303. {
  304. return local_read(&((struct buffer_data_page *)page)->commit)
  305. + BUF_PAGE_HDR_SIZE;
  306. }
  307. /*
  308. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  309. * this issue out.
  310. */
  311. static void free_buffer_page(struct buffer_page *bpage)
  312. {
  313. free_page((unsigned long)bpage->page);
  314. kfree(bpage);
  315. }
  316. /*
  317. * We need to fit the time_stamp delta into 27 bits.
  318. */
  319. static inline int test_time_stamp(u64 delta)
  320. {
  321. if (delta & TS_DELTA_TEST)
  322. return 1;
  323. return 0;
  324. }
  325. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  326. /*
  327. * head_page == tail_page && head == tail then buffer is empty.
  328. */
  329. struct ring_buffer_per_cpu {
  330. int cpu;
  331. struct ring_buffer *buffer;
  332. spinlock_t reader_lock; /* serialize readers */
  333. raw_spinlock_t lock;
  334. struct lock_class_key lock_key;
  335. struct list_head pages;
  336. struct buffer_page *head_page; /* read from head */
  337. struct buffer_page *tail_page; /* write to tail */
  338. struct buffer_page *commit_page; /* committed pages */
  339. struct buffer_page *reader_page;
  340. unsigned long overrun;
  341. unsigned long entries;
  342. u64 write_stamp;
  343. u64 read_stamp;
  344. atomic_t record_disabled;
  345. };
  346. struct ring_buffer {
  347. unsigned pages;
  348. unsigned flags;
  349. int cpus;
  350. atomic_t record_disabled;
  351. cpumask_var_t cpumask;
  352. struct mutex mutex;
  353. struct ring_buffer_per_cpu **buffers;
  354. #ifdef CONFIG_HOTPLUG_CPU
  355. struct notifier_block cpu_notify;
  356. #endif
  357. u64 (*clock)(void);
  358. };
  359. struct ring_buffer_iter {
  360. struct ring_buffer_per_cpu *cpu_buffer;
  361. unsigned long head;
  362. struct buffer_page *head_page;
  363. u64 read_stamp;
  364. };
  365. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  366. #define RB_WARN_ON(buffer, cond) \
  367. ({ \
  368. int _____ret = unlikely(cond); \
  369. if (_____ret) { \
  370. atomic_inc(&buffer->record_disabled); \
  371. WARN_ON(1); \
  372. } \
  373. _____ret; \
  374. })
  375. /* Up this if you want to test the TIME_EXTENTS and normalization */
  376. #define DEBUG_SHIFT 0
  377. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  378. {
  379. u64 time;
  380. preempt_disable_notrace();
  381. /* shift to debug/test normalization and TIME_EXTENTS */
  382. time = buffer->clock() << DEBUG_SHIFT;
  383. preempt_enable_no_resched_notrace();
  384. return time;
  385. }
  386. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  387. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  388. int cpu, u64 *ts)
  389. {
  390. /* Just stupid testing the normalize function and deltas */
  391. *ts >>= DEBUG_SHIFT;
  392. }
  393. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  394. /**
  395. * check_pages - integrity check of buffer pages
  396. * @cpu_buffer: CPU buffer with pages to test
  397. *
  398. * As a safety measure we check to make sure the data pages have not
  399. * been corrupted.
  400. */
  401. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  402. {
  403. struct list_head *head = &cpu_buffer->pages;
  404. struct buffer_page *bpage, *tmp;
  405. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  406. return -1;
  407. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  408. return -1;
  409. list_for_each_entry_safe(bpage, tmp, head, list) {
  410. if (RB_WARN_ON(cpu_buffer,
  411. bpage->list.next->prev != &bpage->list))
  412. return -1;
  413. if (RB_WARN_ON(cpu_buffer,
  414. bpage->list.prev->next != &bpage->list))
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  420. unsigned nr_pages)
  421. {
  422. struct list_head *head = &cpu_buffer->pages;
  423. struct buffer_page *bpage, *tmp;
  424. unsigned long addr;
  425. LIST_HEAD(pages);
  426. unsigned i;
  427. for (i = 0; i < nr_pages; i++) {
  428. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  429. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  430. if (!bpage)
  431. goto free_pages;
  432. list_add(&bpage->list, &pages);
  433. addr = __get_free_page(GFP_KERNEL);
  434. if (!addr)
  435. goto free_pages;
  436. bpage->page = (void *)addr;
  437. rb_init_page(bpage->page);
  438. }
  439. list_splice(&pages, head);
  440. rb_check_pages(cpu_buffer);
  441. return 0;
  442. free_pages:
  443. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  444. list_del_init(&bpage->list);
  445. free_buffer_page(bpage);
  446. }
  447. return -ENOMEM;
  448. }
  449. static struct ring_buffer_per_cpu *
  450. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  451. {
  452. struct ring_buffer_per_cpu *cpu_buffer;
  453. struct buffer_page *bpage;
  454. unsigned long addr;
  455. int ret;
  456. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  457. GFP_KERNEL, cpu_to_node(cpu));
  458. if (!cpu_buffer)
  459. return NULL;
  460. cpu_buffer->cpu = cpu;
  461. cpu_buffer->buffer = buffer;
  462. spin_lock_init(&cpu_buffer->reader_lock);
  463. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  464. INIT_LIST_HEAD(&cpu_buffer->pages);
  465. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  466. GFP_KERNEL, cpu_to_node(cpu));
  467. if (!bpage)
  468. goto fail_free_buffer;
  469. cpu_buffer->reader_page = bpage;
  470. addr = __get_free_page(GFP_KERNEL);
  471. if (!addr)
  472. goto fail_free_reader;
  473. bpage->page = (void *)addr;
  474. rb_init_page(bpage->page);
  475. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  476. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  477. if (ret < 0)
  478. goto fail_free_reader;
  479. cpu_buffer->head_page
  480. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  481. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  482. return cpu_buffer;
  483. fail_free_reader:
  484. free_buffer_page(cpu_buffer->reader_page);
  485. fail_free_buffer:
  486. kfree(cpu_buffer);
  487. return NULL;
  488. }
  489. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  490. {
  491. struct list_head *head = &cpu_buffer->pages;
  492. struct buffer_page *bpage, *tmp;
  493. free_buffer_page(cpu_buffer->reader_page);
  494. list_for_each_entry_safe(bpage, tmp, head, list) {
  495. list_del_init(&bpage->list);
  496. free_buffer_page(bpage);
  497. }
  498. kfree(cpu_buffer);
  499. }
  500. /*
  501. * Causes compile errors if the struct buffer_page gets bigger
  502. * than the struct page.
  503. */
  504. extern int ring_buffer_page_too_big(void);
  505. #ifdef CONFIG_HOTPLUG_CPU
  506. static int rb_cpu_notify(struct notifier_block *self,
  507. unsigned long action, void *hcpu);
  508. #endif
  509. /**
  510. * ring_buffer_alloc - allocate a new ring_buffer
  511. * @size: the size in bytes per cpu that is needed.
  512. * @flags: attributes to set for the ring buffer.
  513. *
  514. * Currently the only flag that is available is the RB_FL_OVERWRITE
  515. * flag. This flag means that the buffer will overwrite old data
  516. * when the buffer wraps. If this flag is not set, the buffer will
  517. * drop data when the tail hits the head.
  518. */
  519. struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
  520. {
  521. struct ring_buffer *buffer;
  522. int bsize;
  523. int cpu;
  524. /* Paranoid! Optimizes out when all is well */
  525. if (sizeof(struct buffer_page) > sizeof(struct page))
  526. ring_buffer_page_too_big();
  527. /* keep it in its own cache line */
  528. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  529. GFP_KERNEL);
  530. if (!buffer)
  531. return NULL;
  532. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  533. goto fail_free_buffer;
  534. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  535. buffer->flags = flags;
  536. buffer->clock = trace_clock_local;
  537. /* need at least two pages */
  538. if (buffer->pages == 1)
  539. buffer->pages++;
  540. /*
  541. * In case of non-hotplug cpu, if the ring-buffer is allocated
  542. * in early initcall, it will not be notified of secondary cpus.
  543. * In that off case, we need to allocate for all possible cpus.
  544. */
  545. #ifdef CONFIG_HOTPLUG_CPU
  546. get_online_cpus();
  547. cpumask_copy(buffer->cpumask, cpu_online_mask);
  548. #else
  549. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  550. #endif
  551. buffer->cpus = nr_cpu_ids;
  552. bsize = sizeof(void *) * nr_cpu_ids;
  553. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  554. GFP_KERNEL);
  555. if (!buffer->buffers)
  556. goto fail_free_cpumask;
  557. for_each_buffer_cpu(buffer, cpu) {
  558. buffer->buffers[cpu] =
  559. rb_allocate_cpu_buffer(buffer, cpu);
  560. if (!buffer->buffers[cpu])
  561. goto fail_free_buffers;
  562. }
  563. #ifdef CONFIG_HOTPLUG_CPU
  564. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  565. buffer->cpu_notify.priority = 0;
  566. register_cpu_notifier(&buffer->cpu_notify);
  567. #endif
  568. put_online_cpus();
  569. mutex_init(&buffer->mutex);
  570. return buffer;
  571. fail_free_buffers:
  572. for_each_buffer_cpu(buffer, cpu) {
  573. if (buffer->buffers[cpu])
  574. rb_free_cpu_buffer(buffer->buffers[cpu]);
  575. }
  576. kfree(buffer->buffers);
  577. fail_free_cpumask:
  578. free_cpumask_var(buffer->cpumask);
  579. put_online_cpus();
  580. fail_free_buffer:
  581. kfree(buffer);
  582. return NULL;
  583. }
  584. EXPORT_SYMBOL_GPL(ring_buffer_alloc);
  585. /**
  586. * ring_buffer_free - free a ring buffer.
  587. * @buffer: the buffer to free.
  588. */
  589. void
  590. ring_buffer_free(struct ring_buffer *buffer)
  591. {
  592. int cpu;
  593. get_online_cpus();
  594. #ifdef CONFIG_HOTPLUG_CPU
  595. unregister_cpu_notifier(&buffer->cpu_notify);
  596. #endif
  597. for_each_buffer_cpu(buffer, cpu)
  598. rb_free_cpu_buffer(buffer->buffers[cpu]);
  599. put_online_cpus();
  600. free_cpumask_var(buffer->cpumask);
  601. kfree(buffer);
  602. }
  603. EXPORT_SYMBOL_GPL(ring_buffer_free);
  604. void ring_buffer_set_clock(struct ring_buffer *buffer,
  605. u64 (*clock)(void))
  606. {
  607. buffer->clock = clock;
  608. }
  609. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  610. static void
  611. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  612. {
  613. struct buffer_page *bpage;
  614. struct list_head *p;
  615. unsigned i;
  616. atomic_inc(&cpu_buffer->record_disabled);
  617. synchronize_sched();
  618. for (i = 0; i < nr_pages; i++) {
  619. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  620. return;
  621. p = cpu_buffer->pages.next;
  622. bpage = list_entry(p, struct buffer_page, list);
  623. list_del_init(&bpage->list);
  624. free_buffer_page(bpage);
  625. }
  626. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  627. return;
  628. rb_reset_cpu(cpu_buffer);
  629. rb_check_pages(cpu_buffer);
  630. atomic_dec(&cpu_buffer->record_disabled);
  631. }
  632. static void
  633. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  634. struct list_head *pages, unsigned nr_pages)
  635. {
  636. struct buffer_page *bpage;
  637. struct list_head *p;
  638. unsigned i;
  639. atomic_inc(&cpu_buffer->record_disabled);
  640. synchronize_sched();
  641. for (i = 0; i < nr_pages; i++) {
  642. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  643. return;
  644. p = pages->next;
  645. bpage = list_entry(p, struct buffer_page, list);
  646. list_del_init(&bpage->list);
  647. list_add_tail(&bpage->list, &cpu_buffer->pages);
  648. }
  649. rb_reset_cpu(cpu_buffer);
  650. rb_check_pages(cpu_buffer);
  651. atomic_dec(&cpu_buffer->record_disabled);
  652. }
  653. /**
  654. * ring_buffer_resize - resize the ring buffer
  655. * @buffer: the buffer to resize.
  656. * @size: the new size.
  657. *
  658. * The tracer is responsible for making sure that the buffer is
  659. * not being used while changing the size.
  660. * Note: We may be able to change the above requirement by using
  661. * RCU synchronizations.
  662. *
  663. * Minimum size is 2 * BUF_PAGE_SIZE.
  664. *
  665. * Returns -1 on failure.
  666. */
  667. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  668. {
  669. struct ring_buffer_per_cpu *cpu_buffer;
  670. unsigned nr_pages, rm_pages, new_pages;
  671. struct buffer_page *bpage, *tmp;
  672. unsigned long buffer_size;
  673. unsigned long addr;
  674. LIST_HEAD(pages);
  675. int i, cpu;
  676. /*
  677. * Always succeed at resizing a non-existent buffer:
  678. */
  679. if (!buffer)
  680. return size;
  681. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  682. size *= BUF_PAGE_SIZE;
  683. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  684. /* we need a minimum of two pages */
  685. if (size < BUF_PAGE_SIZE * 2)
  686. size = BUF_PAGE_SIZE * 2;
  687. if (size == buffer_size)
  688. return size;
  689. mutex_lock(&buffer->mutex);
  690. get_online_cpus();
  691. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  692. if (size < buffer_size) {
  693. /* easy case, just free pages */
  694. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  695. goto out_fail;
  696. rm_pages = buffer->pages - nr_pages;
  697. for_each_buffer_cpu(buffer, cpu) {
  698. cpu_buffer = buffer->buffers[cpu];
  699. rb_remove_pages(cpu_buffer, rm_pages);
  700. }
  701. goto out;
  702. }
  703. /*
  704. * This is a bit more difficult. We only want to add pages
  705. * when we can allocate enough for all CPUs. We do this
  706. * by allocating all the pages and storing them on a local
  707. * link list. If we succeed in our allocation, then we
  708. * add these pages to the cpu_buffers. Otherwise we just free
  709. * them all and return -ENOMEM;
  710. */
  711. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  712. goto out_fail;
  713. new_pages = nr_pages - buffer->pages;
  714. for_each_buffer_cpu(buffer, cpu) {
  715. for (i = 0; i < new_pages; i++) {
  716. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  717. cache_line_size()),
  718. GFP_KERNEL, cpu_to_node(cpu));
  719. if (!bpage)
  720. goto free_pages;
  721. list_add(&bpage->list, &pages);
  722. addr = __get_free_page(GFP_KERNEL);
  723. if (!addr)
  724. goto free_pages;
  725. bpage->page = (void *)addr;
  726. rb_init_page(bpage->page);
  727. }
  728. }
  729. for_each_buffer_cpu(buffer, cpu) {
  730. cpu_buffer = buffer->buffers[cpu];
  731. rb_insert_pages(cpu_buffer, &pages, new_pages);
  732. }
  733. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  734. goto out_fail;
  735. out:
  736. buffer->pages = nr_pages;
  737. put_online_cpus();
  738. mutex_unlock(&buffer->mutex);
  739. return size;
  740. free_pages:
  741. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  742. list_del_init(&bpage->list);
  743. free_buffer_page(bpage);
  744. }
  745. put_online_cpus();
  746. mutex_unlock(&buffer->mutex);
  747. return -ENOMEM;
  748. /*
  749. * Something went totally wrong, and we are too paranoid
  750. * to even clean up the mess.
  751. */
  752. out_fail:
  753. put_online_cpus();
  754. mutex_unlock(&buffer->mutex);
  755. return -1;
  756. }
  757. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  758. static inline void *
  759. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  760. {
  761. return bpage->data + index;
  762. }
  763. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  764. {
  765. return bpage->page->data + index;
  766. }
  767. static inline struct ring_buffer_event *
  768. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  769. {
  770. return __rb_page_index(cpu_buffer->reader_page,
  771. cpu_buffer->reader_page->read);
  772. }
  773. static inline struct ring_buffer_event *
  774. rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
  775. {
  776. return __rb_page_index(cpu_buffer->head_page,
  777. cpu_buffer->head_page->read);
  778. }
  779. static inline struct ring_buffer_event *
  780. rb_iter_head_event(struct ring_buffer_iter *iter)
  781. {
  782. return __rb_page_index(iter->head_page, iter->head);
  783. }
  784. static inline unsigned rb_page_write(struct buffer_page *bpage)
  785. {
  786. return local_read(&bpage->write);
  787. }
  788. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  789. {
  790. return local_read(&bpage->page->commit);
  791. }
  792. /* Size is determined by what has been commited */
  793. static inline unsigned rb_page_size(struct buffer_page *bpage)
  794. {
  795. return rb_page_commit(bpage);
  796. }
  797. static inline unsigned
  798. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  799. {
  800. return rb_page_commit(cpu_buffer->commit_page);
  801. }
  802. static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
  803. {
  804. return rb_page_commit(cpu_buffer->head_page);
  805. }
  806. /*
  807. * When the tail hits the head and the buffer is in overwrite mode,
  808. * the head jumps to the next page and all content on the previous
  809. * page is discarded. But before doing so, we update the overrun
  810. * variable of the buffer.
  811. */
  812. static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
  813. {
  814. struct ring_buffer_event *event;
  815. unsigned long head;
  816. for (head = 0; head < rb_head_size(cpu_buffer);
  817. head += rb_event_length(event)) {
  818. event = __rb_page_index(cpu_buffer->head_page, head);
  819. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  820. return;
  821. /* Only count data entries */
  822. if (event->type != RINGBUF_TYPE_DATA)
  823. continue;
  824. cpu_buffer->overrun++;
  825. cpu_buffer->entries--;
  826. }
  827. }
  828. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  829. struct buffer_page **bpage)
  830. {
  831. struct list_head *p = (*bpage)->list.next;
  832. if (p == &cpu_buffer->pages)
  833. p = p->next;
  834. *bpage = list_entry(p, struct buffer_page, list);
  835. }
  836. static inline unsigned
  837. rb_event_index(struct ring_buffer_event *event)
  838. {
  839. unsigned long addr = (unsigned long)event;
  840. return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
  841. }
  842. static int
  843. rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  844. struct ring_buffer_event *event)
  845. {
  846. unsigned long addr = (unsigned long)event;
  847. unsigned long index;
  848. index = rb_event_index(event);
  849. addr &= PAGE_MASK;
  850. return cpu_buffer->commit_page->page == (void *)addr &&
  851. rb_commit_index(cpu_buffer) == index;
  852. }
  853. static void
  854. rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
  855. struct ring_buffer_event *event)
  856. {
  857. unsigned long addr = (unsigned long)event;
  858. unsigned long index;
  859. index = rb_event_index(event);
  860. addr &= PAGE_MASK;
  861. while (cpu_buffer->commit_page->page != (void *)addr) {
  862. if (RB_WARN_ON(cpu_buffer,
  863. cpu_buffer->commit_page == cpu_buffer->tail_page))
  864. return;
  865. cpu_buffer->commit_page->page->commit =
  866. cpu_buffer->commit_page->write;
  867. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  868. cpu_buffer->write_stamp =
  869. cpu_buffer->commit_page->page->time_stamp;
  870. }
  871. /* Now set the commit to the event's index */
  872. local_set(&cpu_buffer->commit_page->page->commit, index);
  873. }
  874. static void
  875. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  876. {
  877. /*
  878. * We only race with interrupts and NMIs on this CPU.
  879. * If we own the commit event, then we can commit
  880. * all others that interrupted us, since the interruptions
  881. * are in stack format (they finish before they come
  882. * back to us). This allows us to do a simple loop to
  883. * assign the commit to the tail.
  884. */
  885. again:
  886. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  887. cpu_buffer->commit_page->page->commit =
  888. cpu_buffer->commit_page->write;
  889. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  890. cpu_buffer->write_stamp =
  891. cpu_buffer->commit_page->page->time_stamp;
  892. /* add barrier to keep gcc from optimizing too much */
  893. barrier();
  894. }
  895. while (rb_commit_index(cpu_buffer) !=
  896. rb_page_write(cpu_buffer->commit_page)) {
  897. cpu_buffer->commit_page->page->commit =
  898. cpu_buffer->commit_page->write;
  899. barrier();
  900. }
  901. /* again, keep gcc from optimizing */
  902. barrier();
  903. /*
  904. * If an interrupt came in just after the first while loop
  905. * and pushed the tail page forward, we will be left with
  906. * a dangling commit that will never go forward.
  907. */
  908. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  909. goto again;
  910. }
  911. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  912. {
  913. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  914. cpu_buffer->reader_page->read = 0;
  915. }
  916. static void rb_inc_iter(struct ring_buffer_iter *iter)
  917. {
  918. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  919. /*
  920. * The iterator could be on the reader page (it starts there).
  921. * But the head could have moved, since the reader was
  922. * found. Check for this case and assign the iterator
  923. * to the head page instead of next.
  924. */
  925. if (iter->head_page == cpu_buffer->reader_page)
  926. iter->head_page = cpu_buffer->head_page;
  927. else
  928. rb_inc_page(cpu_buffer, &iter->head_page);
  929. iter->read_stamp = iter->head_page->page->time_stamp;
  930. iter->head = 0;
  931. }
  932. /**
  933. * ring_buffer_update_event - update event type and data
  934. * @event: the even to update
  935. * @type: the type of event
  936. * @length: the size of the event field in the ring buffer
  937. *
  938. * Update the type and data fields of the event. The length
  939. * is the actual size that is written to the ring buffer,
  940. * and with this, we can determine what to place into the
  941. * data field.
  942. */
  943. static void
  944. rb_update_event(struct ring_buffer_event *event,
  945. unsigned type, unsigned length)
  946. {
  947. event->type = type;
  948. switch (type) {
  949. case RINGBUF_TYPE_PADDING:
  950. break;
  951. case RINGBUF_TYPE_TIME_EXTEND:
  952. event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
  953. break;
  954. case RINGBUF_TYPE_TIME_STAMP:
  955. event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
  956. break;
  957. case RINGBUF_TYPE_DATA:
  958. length -= RB_EVNT_HDR_SIZE;
  959. if (length > RB_MAX_SMALL_DATA) {
  960. event->len = 0;
  961. event->array[0] = length;
  962. } else
  963. event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  964. break;
  965. default:
  966. BUG();
  967. }
  968. }
  969. static unsigned rb_calculate_event_length(unsigned length)
  970. {
  971. struct ring_buffer_event event; /* Used only for sizeof array */
  972. /* zero length can cause confusions */
  973. if (!length)
  974. length = 1;
  975. if (length > RB_MAX_SMALL_DATA)
  976. length += sizeof(event.array[0]);
  977. length += RB_EVNT_HDR_SIZE;
  978. length = ALIGN(length, RB_ALIGNMENT);
  979. return length;
  980. }
  981. static struct ring_buffer_event *
  982. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  983. unsigned type, unsigned long length, u64 *ts)
  984. {
  985. struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
  986. unsigned long tail, write;
  987. struct ring_buffer *buffer = cpu_buffer->buffer;
  988. struct ring_buffer_event *event;
  989. unsigned long flags;
  990. bool lock_taken = false;
  991. commit_page = cpu_buffer->commit_page;
  992. /* we just need to protect against interrupts */
  993. barrier();
  994. tail_page = cpu_buffer->tail_page;
  995. write = local_add_return(length, &tail_page->write);
  996. tail = write - length;
  997. /* See if we shot pass the end of this buffer page */
  998. if (write > BUF_PAGE_SIZE) {
  999. struct buffer_page *next_page = tail_page;
  1000. local_irq_save(flags);
  1001. /*
  1002. * Since the write to the buffer is still not
  1003. * fully lockless, we must be careful with NMIs.
  1004. * The locks in the writers are taken when a write
  1005. * crosses to a new page. The locks protect against
  1006. * races with the readers (this will soon be fixed
  1007. * with a lockless solution).
  1008. *
  1009. * Because we can not protect against NMIs, and we
  1010. * want to keep traces reentrant, we need to manage
  1011. * what happens when we are in an NMI.
  1012. *
  1013. * NMIs can happen after we take the lock.
  1014. * If we are in an NMI, only take the lock
  1015. * if it is not already taken. Otherwise
  1016. * simply fail.
  1017. */
  1018. if (unlikely(in_nmi())) {
  1019. if (!__raw_spin_trylock(&cpu_buffer->lock))
  1020. goto out_reset;
  1021. } else
  1022. __raw_spin_lock(&cpu_buffer->lock);
  1023. lock_taken = true;
  1024. rb_inc_page(cpu_buffer, &next_page);
  1025. head_page = cpu_buffer->head_page;
  1026. reader_page = cpu_buffer->reader_page;
  1027. /* we grabbed the lock before incrementing */
  1028. if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
  1029. goto out_reset;
  1030. /*
  1031. * If for some reason, we had an interrupt storm that made
  1032. * it all the way around the buffer, bail, and warn
  1033. * about it.
  1034. */
  1035. if (unlikely(next_page == commit_page)) {
  1036. WARN_ON_ONCE(1);
  1037. goto out_reset;
  1038. }
  1039. if (next_page == head_page) {
  1040. if (!(buffer->flags & RB_FL_OVERWRITE))
  1041. goto out_reset;
  1042. /* tail_page has not moved yet? */
  1043. if (tail_page == cpu_buffer->tail_page) {
  1044. /* count overflows */
  1045. rb_update_overflow(cpu_buffer);
  1046. rb_inc_page(cpu_buffer, &head_page);
  1047. cpu_buffer->head_page = head_page;
  1048. cpu_buffer->head_page->read = 0;
  1049. }
  1050. }
  1051. /*
  1052. * If the tail page is still the same as what we think
  1053. * it is, then it is up to us to update the tail
  1054. * pointer.
  1055. */
  1056. if (tail_page == cpu_buffer->tail_page) {
  1057. local_set(&next_page->write, 0);
  1058. local_set(&next_page->page->commit, 0);
  1059. cpu_buffer->tail_page = next_page;
  1060. /* reread the time stamp */
  1061. *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
  1062. cpu_buffer->tail_page->page->time_stamp = *ts;
  1063. }
  1064. /*
  1065. * The actual tail page has moved forward.
  1066. */
  1067. if (tail < BUF_PAGE_SIZE) {
  1068. /* Mark the rest of the page with padding */
  1069. event = __rb_page_index(tail_page, tail);
  1070. rb_event_set_padding(event);
  1071. }
  1072. if (tail <= BUF_PAGE_SIZE)
  1073. /* Set the write back to the previous setting */
  1074. local_set(&tail_page->write, tail);
  1075. /*
  1076. * If this was a commit entry that failed,
  1077. * increment that too
  1078. */
  1079. if (tail_page == cpu_buffer->commit_page &&
  1080. tail == rb_commit_index(cpu_buffer)) {
  1081. rb_set_commit_to_write(cpu_buffer);
  1082. }
  1083. __raw_spin_unlock(&cpu_buffer->lock);
  1084. local_irq_restore(flags);
  1085. /* fail and let the caller try again */
  1086. return ERR_PTR(-EAGAIN);
  1087. }
  1088. /* We reserved something on the buffer */
  1089. if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
  1090. return NULL;
  1091. event = __rb_page_index(tail_page, tail);
  1092. rb_update_event(event, type, length);
  1093. /*
  1094. * If this is a commit and the tail is zero, then update
  1095. * this page's time stamp.
  1096. */
  1097. if (!tail && rb_is_commit(cpu_buffer, event))
  1098. cpu_buffer->commit_page->page->time_stamp = *ts;
  1099. return event;
  1100. out_reset:
  1101. /* reset write */
  1102. if (tail <= BUF_PAGE_SIZE)
  1103. local_set(&tail_page->write, tail);
  1104. if (likely(lock_taken))
  1105. __raw_spin_unlock(&cpu_buffer->lock);
  1106. local_irq_restore(flags);
  1107. return NULL;
  1108. }
  1109. static int
  1110. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1111. u64 *ts, u64 *delta)
  1112. {
  1113. struct ring_buffer_event *event;
  1114. static int once;
  1115. int ret;
  1116. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1117. printk(KERN_WARNING "Delta way too big! %llu"
  1118. " ts=%llu write stamp = %llu\n",
  1119. (unsigned long long)*delta,
  1120. (unsigned long long)*ts,
  1121. (unsigned long long)cpu_buffer->write_stamp);
  1122. WARN_ON(1);
  1123. }
  1124. /*
  1125. * The delta is too big, we to add a
  1126. * new timestamp.
  1127. */
  1128. event = __rb_reserve_next(cpu_buffer,
  1129. RINGBUF_TYPE_TIME_EXTEND,
  1130. RB_LEN_TIME_EXTEND,
  1131. ts);
  1132. if (!event)
  1133. return -EBUSY;
  1134. if (PTR_ERR(event) == -EAGAIN)
  1135. return -EAGAIN;
  1136. /* Only a commited time event can update the write stamp */
  1137. if (rb_is_commit(cpu_buffer, event)) {
  1138. /*
  1139. * If this is the first on the page, then we need to
  1140. * update the page itself, and just put in a zero.
  1141. */
  1142. if (rb_event_index(event)) {
  1143. event->time_delta = *delta & TS_MASK;
  1144. event->array[0] = *delta >> TS_SHIFT;
  1145. } else {
  1146. cpu_buffer->commit_page->page->time_stamp = *ts;
  1147. event->time_delta = 0;
  1148. event->array[0] = 0;
  1149. }
  1150. cpu_buffer->write_stamp = *ts;
  1151. /* let the caller know this was the commit */
  1152. ret = 1;
  1153. } else {
  1154. /* Darn, this is just wasted space */
  1155. event->time_delta = 0;
  1156. event->array[0] = 0;
  1157. ret = 0;
  1158. }
  1159. *delta = 0;
  1160. return ret;
  1161. }
  1162. static struct ring_buffer_event *
  1163. rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
  1164. unsigned type, unsigned long length)
  1165. {
  1166. struct ring_buffer_event *event;
  1167. u64 ts, delta;
  1168. int commit = 0;
  1169. int nr_loops = 0;
  1170. again:
  1171. /*
  1172. * We allow for interrupts to reenter here and do a trace.
  1173. * If one does, it will cause this original code to loop
  1174. * back here. Even with heavy interrupts happening, this
  1175. * should only happen a few times in a row. If this happens
  1176. * 1000 times in a row, there must be either an interrupt
  1177. * storm or we have something buggy.
  1178. * Bail!
  1179. */
  1180. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1181. return NULL;
  1182. ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
  1183. /*
  1184. * Only the first commit can update the timestamp.
  1185. * Yes there is a race here. If an interrupt comes in
  1186. * just after the conditional and it traces too, then it
  1187. * will also check the deltas. More than one timestamp may
  1188. * also be made. But only the entry that did the actual
  1189. * commit will be something other than zero.
  1190. */
  1191. if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1192. rb_page_write(cpu_buffer->tail_page) ==
  1193. rb_commit_index(cpu_buffer)) {
  1194. delta = ts - cpu_buffer->write_stamp;
  1195. /* make sure this delta is calculated here */
  1196. barrier();
  1197. /* Did the write stamp get updated already? */
  1198. if (unlikely(ts < cpu_buffer->write_stamp))
  1199. delta = 0;
  1200. if (test_time_stamp(delta)) {
  1201. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1202. if (commit == -EBUSY)
  1203. return NULL;
  1204. if (commit == -EAGAIN)
  1205. goto again;
  1206. RB_WARN_ON(cpu_buffer, commit < 0);
  1207. }
  1208. } else
  1209. /* Non commits have zero deltas */
  1210. delta = 0;
  1211. event = __rb_reserve_next(cpu_buffer, type, length, &ts);
  1212. if (PTR_ERR(event) == -EAGAIN)
  1213. goto again;
  1214. if (!event) {
  1215. if (unlikely(commit))
  1216. /*
  1217. * Ouch! We needed a timestamp and it was commited. But
  1218. * we didn't get our event reserved.
  1219. */
  1220. rb_set_commit_to_write(cpu_buffer);
  1221. return NULL;
  1222. }
  1223. /*
  1224. * If the timestamp was commited, make the commit our entry
  1225. * now so that we will update it when needed.
  1226. */
  1227. if (commit)
  1228. rb_set_commit_event(cpu_buffer, event);
  1229. else if (!rb_is_commit(cpu_buffer, event))
  1230. delta = 0;
  1231. event->time_delta = delta;
  1232. return event;
  1233. }
  1234. static DEFINE_PER_CPU(int, rb_need_resched);
  1235. /**
  1236. * ring_buffer_lock_reserve - reserve a part of the buffer
  1237. * @buffer: the ring buffer to reserve from
  1238. * @length: the length of the data to reserve (excluding event header)
  1239. *
  1240. * Returns a reseverd event on the ring buffer to copy directly to.
  1241. * The user of this interface will need to get the body to write into
  1242. * and can use the ring_buffer_event_data() interface.
  1243. *
  1244. * The length is the length of the data needed, not the event length
  1245. * which also includes the event header.
  1246. *
  1247. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1248. * If NULL is returned, then nothing has been allocated or locked.
  1249. */
  1250. struct ring_buffer_event *
  1251. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1252. {
  1253. struct ring_buffer_per_cpu *cpu_buffer;
  1254. struct ring_buffer_event *event;
  1255. int cpu, resched;
  1256. if (ring_buffer_flags != RB_BUFFERS_ON)
  1257. return NULL;
  1258. if (atomic_read(&buffer->record_disabled))
  1259. return NULL;
  1260. /* If we are tracing schedule, we don't want to recurse */
  1261. resched = ftrace_preempt_disable();
  1262. cpu = raw_smp_processor_id();
  1263. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1264. goto out;
  1265. cpu_buffer = buffer->buffers[cpu];
  1266. if (atomic_read(&cpu_buffer->record_disabled))
  1267. goto out;
  1268. length = rb_calculate_event_length(length);
  1269. if (length > BUF_PAGE_SIZE)
  1270. goto out;
  1271. event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
  1272. if (!event)
  1273. goto out;
  1274. /*
  1275. * Need to store resched state on this cpu.
  1276. * Only the first needs to.
  1277. */
  1278. if (preempt_count() == 1)
  1279. per_cpu(rb_need_resched, cpu) = resched;
  1280. return event;
  1281. out:
  1282. ftrace_preempt_enable(resched);
  1283. return NULL;
  1284. }
  1285. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1286. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1287. struct ring_buffer_event *event)
  1288. {
  1289. cpu_buffer->entries++;
  1290. /* Only process further if we own the commit */
  1291. if (!rb_is_commit(cpu_buffer, event))
  1292. return;
  1293. cpu_buffer->write_stamp += event->time_delta;
  1294. rb_set_commit_to_write(cpu_buffer);
  1295. }
  1296. /**
  1297. * ring_buffer_unlock_commit - commit a reserved
  1298. * @buffer: The buffer to commit to
  1299. * @event: The event pointer to commit.
  1300. *
  1301. * This commits the data to the ring buffer, and releases any locks held.
  1302. *
  1303. * Must be paired with ring_buffer_lock_reserve.
  1304. */
  1305. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1306. struct ring_buffer_event *event)
  1307. {
  1308. struct ring_buffer_per_cpu *cpu_buffer;
  1309. int cpu = raw_smp_processor_id();
  1310. cpu_buffer = buffer->buffers[cpu];
  1311. rb_commit(cpu_buffer, event);
  1312. /*
  1313. * Only the last preempt count needs to restore preemption.
  1314. */
  1315. if (preempt_count() == 1)
  1316. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1317. else
  1318. preempt_enable_no_resched_notrace();
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1322. /**
  1323. * ring_buffer_write - write data to the buffer without reserving
  1324. * @buffer: The ring buffer to write to.
  1325. * @length: The length of the data being written (excluding the event header)
  1326. * @data: The data to write to the buffer.
  1327. *
  1328. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  1329. * one function. If you already have the data to write to the buffer, it
  1330. * may be easier to simply call this function.
  1331. *
  1332. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  1333. * and not the length of the event which would hold the header.
  1334. */
  1335. int ring_buffer_write(struct ring_buffer *buffer,
  1336. unsigned long length,
  1337. void *data)
  1338. {
  1339. struct ring_buffer_per_cpu *cpu_buffer;
  1340. struct ring_buffer_event *event;
  1341. unsigned long event_length;
  1342. void *body;
  1343. int ret = -EBUSY;
  1344. int cpu, resched;
  1345. if (ring_buffer_flags != RB_BUFFERS_ON)
  1346. return -EBUSY;
  1347. if (atomic_read(&buffer->record_disabled))
  1348. return -EBUSY;
  1349. resched = ftrace_preempt_disable();
  1350. cpu = raw_smp_processor_id();
  1351. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1352. goto out;
  1353. cpu_buffer = buffer->buffers[cpu];
  1354. if (atomic_read(&cpu_buffer->record_disabled))
  1355. goto out;
  1356. event_length = rb_calculate_event_length(length);
  1357. event = rb_reserve_next_event(cpu_buffer,
  1358. RINGBUF_TYPE_DATA, event_length);
  1359. if (!event)
  1360. goto out;
  1361. body = rb_event_data(event);
  1362. memcpy(body, data, length);
  1363. rb_commit(cpu_buffer, event);
  1364. ret = 0;
  1365. out:
  1366. ftrace_preempt_enable(resched);
  1367. return ret;
  1368. }
  1369. EXPORT_SYMBOL_GPL(ring_buffer_write);
  1370. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  1371. {
  1372. struct buffer_page *reader = cpu_buffer->reader_page;
  1373. struct buffer_page *head = cpu_buffer->head_page;
  1374. struct buffer_page *commit = cpu_buffer->commit_page;
  1375. return reader->read == rb_page_commit(reader) &&
  1376. (commit == reader ||
  1377. (commit == head &&
  1378. head->read == rb_page_commit(commit)));
  1379. }
  1380. /**
  1381. * ring_buffer_record_disable - stop all writes into the buffer
  1382. * @buffer: The ring buffer to stop writes to.
  1383. *
  1384. * This prevents all writes to the buffer. Any attempt to write
  1385. * to the buffer after this will fail and return NULL.
  1386. *
  1387. * The caller should call synchronize_sched() after this.
  1388. */
  1389. void ring_buffer_record_disable(struct ring_buffer *buffer)
  1390. {
  1391. atomic_inc(&buffer->record_disabled);
  1392. }
  1393. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  1394. /**
  1395. * ring_buffer_record_enable - enable writes to the buffer
  1396. * @buffer: The ring buffer to enable writes
  1397. *
  1398. * Note, multiple disables will need the same number of enables
  1399. * to truely enable the writing (much like preempt_disable).
  1400. */
  1401. void ring_buffer_record_enable(struct ring_buffer *buffer)
  1402. {
  1403. atomic_dec(&buffer->record_disabled);
  1404. }
  1405. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  1406. /**
  1407. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  1408. * @buffer: The ring buffer to stop writes to.
  1409. * @cpu: The CPU buffer to stop
  1410. *
  1411. * This prevents all writes to the buffer. Any attempt to write
  1412. * to the buffer after this will fail and return NULL.
  1413. *
  1414. * The caller should call synchronize_sched() after this.
  1415. */
  1416. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  1417. {
  1418. struct ring_buffer_per_cpu *cpu_buffer;
  1419. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1420. return;
  1421. cpu_buffer = buffer->buffers[cpu];
  1422. atomic_inc(&cpu_buffer->record_disabled);
  1423. }
  1424. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  1425. /**
  1426. * ring_buffer_record_enable_cpu - enable writes to the buffer
  1427. * @buffer: The ring buffer to enable writes
  1428. * @cpu: The CPU to enable.
  1429. *
  1430. * Note, multiple disables will need the same number of enables
  1431. * to truely enable the writing (much like preempt_disable).
  1432. */
  1433. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  1434. {
  1435. struct ring_buffer_per_cpu *cpu_buffer;
  1436. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1437. return;
  1438. cpu_buffer = buffer->buffers[cpu];
  1439. atomic_dec(&cpu_buffer->record_disabled);
  1440. }
  1441. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  1442. /**
  1443. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  1444. * @buffer: The ring buffer
  1445. * @cpu: The per CPU buffer to get the entries from.
  1446. */
  1447. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  1448. {
  1449. struct ring_buffer_per_cpu *cpu_buffer;
  1450. unsigned long ret;
  1451. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1452. return 0;
  1453. cpu_buffer = buffer->buffers[cpu];
  1454. ret = cpu_buffer->entries;
  1455. return ret;
  1456. }
  1457. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  1458. /**
  1459. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  1460. * @buffer: The ring buffer
  1461. * @cpu: The per CPU buffer to get the number of overruns from
  1462. */
  1463. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1464. {
  1465. struct ring_buffer_per_cpu *cpu_buffer;
  1466. unsigned long ret;
  1467. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1468. return 0;
  1469. cpu_buffer = buffer->buffers[cpu];
  1470. ret = cpu_buffer->overrun;
  1471. return ret;
  1472. }
  1473. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  1474. /**
  1475. * ring_buffer_entries - get the number of entries in a buffer
  1476. * @buffer: The ring buffer
  1477. *
  1478. * Returns the total number of entries in the ring buffer
  1479. * (all CPU entries)
  1480. */
  1481. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  1482. {
  1483. struct ring_buffer_per_cpu *cpu_buffer;
  1484. unsigned long entries = 0;
  1485. int cpu;
  1486. /* if you care about this being correct, lock the buffer */
  1487. for_each_buffer_cpu(buffer, cpu) {
  1488. cpu_buffer = buffer->buffers[cpu];
  1489. entries += cpu_buffer->entries;
  1490. }
  1491. return entries;
  1492. }
  1493. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  1494. /**
  1495. * ring_buffer_overrun_cpu - get the number of overruns in buffer
  1496. * @buffer: The ring buffer
  1497. *
  1498. * Returns the total number of overruns in the ring buffer
  1499. * (all CPU entries)
  1500. */
  1501. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  1502. {
  1503. struct ring_buffer_per_cpu *cpu_buffer;
  1504. unsigned long overruns = 0;
  1505. int cpu;
  1506. /* if you care about this being correct, lock the buffer */
  1507. for_each_buffer_cpu(buffer, cpu) {
  1508. cpu_buffer = buffer->buffers[cpu];
  1509. overruns += cpu_buffer->overrun;
  1510. }
  1511. return overruns;
  1512. }
  1513. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  1514. static void rb_iter_reset(struct ring_buffer_iter *iter)
  1515. {
  1516. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1517. /* Iterator usage is expected to have record disabled */
  1518. if (list_empty(&cpu_buffer->reader_page->list)) {
  1519. iter->head_page = cpu_buffer->head_page;
  1520. iter->head = cpu_buffer->head_page->read;
  1521. } else {
  1522. iter->head_page = cpu_buffer->reader_page;
  1523. iter->head = cpu_buffer->reader_page->read;
  1524. }
  1525. if (iter->head)
  1526. iter->read_stamp = cpu_buffer->read_stamp;
  1527. else
  1528. iter->read_stamp = iter->head_page->page->time_stamp;
  1529. }
  1530. /**
  1531. * ring_buffer_iter_reset - reset an iterator
  1532. * @iter: The iterator to reset
  1533. *
  1534. * Resets the iterator, so that it will start from the beginning
  1535. * again.
  1536. */
  1537. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  1538. {
  1539. struct ring_buffer_per_cpu *cpu_buffer;
  1540. unsigned long flags;
  1541. if (!iter)
  1542. return;
  1543. cpu_buffer = iter->cpu_buffer;
  1544. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1545. rb_iter_reset(iter);
  1546. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1547. }
  1548. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  1549. /**
  1550. * ring_buffer_iter_empty - check if an iterator has no more to read
  1551. * @iter: The iterator to check
  1552. */
  1553. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  1554. {
  1555. struct ring_buffer_per_cpu *cpu_buffer;
  1556. cpu_buffer = iter->cpu_buffer;
  1557. return iter->head_page == cpu_buffer->commit_page &&
  1558. iter->head == rb_commit_index(cpu_buffer);
  1559. }
  1560. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  1561. static void
  1562. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1563. struct ring_buffer_event *event)
  1564. {
  1565. u64 delta;
  1566. switch (event->type) {
  1567. case RINGBUF_TYPE_PADDING:
  1568. return;
  1569. case RINGBUF_TYPE_TIME_EXTEND:
  1570. delta = event->array[0];
  1571. delta <<= TS_SHIFT;
  1572. delta += event->time_delta;
  1573. cpu_buffer->read_stamp += delta;
  1574. return;
  1575. case RINGBUF_TYPE_TIME_STAMP:
  1576. /* FIXME: not implemented */
  1577. return;
  1578. case RINGBUF_TYPE_DATA:
  1579. cpu_buffer->read_stamp += event->time_delta;
  1580. return;
  1581. default:
  1582. BUG();
  1583. }
  1584. return;
  1585. }
  1586. static void
  1587. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  1588. struct ring_buffer_event *event)
  1589. {
  1590. u64 delta;
  1591. switch (event->type) {
  1592. case RINGBUF_TYPE_PADDING:
  1593. return;
  1594. case RINGBUF_TYPE_TIME_EXTEND:
  1595. delta = event->array[0];
  1596. delta <<= TS_SHIFT;
  1597. delta += event->time_delta;
  1598. iter->read_stamp += delta;
  1599. return;
  1600. case RINGBUF_TYPE_TIME_STAMP:
  1601. /* FIXME: not implemented */
  1602. return;
  1603. case RINGBUF_TYPE_DATA:
  1604. iter->read_stamp += event->time_delta;
  1605. return;
  1606. default:
  1607. BUG();
  1608. }
  1609. return;
  1610. }
  1611. static struct buffer_page *
  1612. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1613. {
  1614. struct buffer_page *reader = NULL;
  1615. unsigned long flags;
  1616. int nr_loops = 0;
  1617. local_irq_save(flags);
  1618. __raw_spin_lock(&cpu_buffer->lock);
  1619. again:
  1620. /*
  1621. * This should normally only loop twice. But because the
  1622. * start of the reader inserts an empty page, it causes
  1623. * a case where we will loop three times. There should be no
  1624. * reason to loop four times (that I know of).
  1625. */
  1626. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  1627. reader = NULL;
  1628. goto out;
  1629. }
  1630. reader = cpu_buffer->reader_page;
  1631. /* If there's more to read, return this page */
  1632. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  1633. goto out;
  1634. /* Never should we have an index greater than the size */
  1635. if (RB_WARN_ON(cpu_buffer,
  1636. cpu_buffer->reader_page->read > rb_page_size(reader)))
  1637. goto out;
  1638. /* check if we caught up to the tail */
  1639. reader = NULL;
  1640. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  1641. goto out;
  1642. /*
  1643. * Splice the empty reader page into the list around the head.
  1644. * Reset the reader page to size zero.
  1645. */
  1646. reader = cpu_buffer->head_page;
  1647. cpu_buffer->reader_page->list.next = reader->list.next;
  1648. cpu_buffer->reader_page->list.prev = reader->list.prev;
  1649. local_set(&cpu_buffer->reader_page->write, 0);
  1650. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1651. /* Make the reader page now replace the head */
  1652. reader->list.prev->next = &cpu_buffer->reader_page->list;
  1653. reader->list.next->prev = &cpu_buffer->reader_page->list;
  1654. /*
  1655. * If the tail is on the reader, then we must set the head
  1656. * to the inserted page, otherwise we set it one before.
  1657. */
  1658. cpu_buffer->head_page = cpu_buffer->reader_page;
  1659. if (cpu_buffer->commit_page != reader)
  1660. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  1661. /* Finally update the reader page to the new head */
  1662. cpu_buffer->reader_page = reader;
  1663. rb_reset_reader_page(cpu_buffer);
  1664. goto again;
  1665. out:
  1666. __raw_spin_unlock(&cpu_buffer->lock);
  1667. local_irq_restore(flags);
  1668. return reader;
  1669. }
  1670. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  1671. {
  1672. struct ring_buffer_event *event;
  1673. struct buffer_page *reader;
  1674. unsigned length;
  1675. reader = rb_get_reader_page(cpu_buffer);
  1676. /* This function should not be called when buffer is empty */
  1677. if (RB_WARN_ON(cpu_buffer, !reader))
  1678. return;
  1679. event = rb_reader_event(cpu_buffer);
  1680. if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
  1681. cpu_buffer->entries--;
  1682. rb_update_read_stamp(cpu_buffer, event);
  1683. length = rb_event_length(event);
  1684. cpu_buffer->reader_page->read += length;
  1685. }
  1686. static void rb_advance_iter(struct ring_buffer_iter *iter)
  1687. {
  1688. struct ring_buffer *buffer;
  1689. struct ring_buffer_per_cpu *cpu_buffer;
  1690. struct ring_buffer_event *event;
  1691. unsigned length;
  1692. cpu_buffer = iter->cpu_buffer;
  1693. buffer = cpu_buffer->buffer;
  1694. /*
  1695. * Check if we are at the end of the buffer.
  1696. */
  1697. if (iter->head >= rb_page_size(iter->head_page)) {
  1698. if (RB_WARN_ON(buffer,
  1699. iter->head_page == cpu_buffer->commit_page))
  1700. return;
  1701. rb_inc_iter(iter);
  1702. return;
  1703. }
  1704. event = rb_iter_head_event(iter);
  1705. length = rb_event_length(event);
  1706. /*
  1707. * This should not be called to advance the header if we are
  1708. * at the tail of the buffer.
  1709. */
  1710. if (RB_WARN_ON(cpu_buffer,
  1711. (iter->head_page == cpu_buffer->commit_page) &&
  1712. (iter->head + length > rb_commit_index(cpu_buffer))))
  1713. return;
  1714. rb_update_iter_read_stamp(iter, event);
  1715. iter->head += length;
  1716. /* check for end of page padding */
  1717. if ((iter->head >= rb_page_size(iter->head_page)) &&
  1718. (iter->head_page != cpu_buffer->commit_page))
  1719. rb_advance_iter(iter);
  1720. }
  1721. static struct ring_buffer_event *
  1722. rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1723. {
  1724. struct ring_buffer_per_cpu *cpu_buffer;
  1725. struct ring_buffer_event *event;
  1726. struct buffer_page *reader;
  1727. int nr_loops = 0;
  1728. cpu_buffer = buffer->buffers[cpu];
  1729. again:
  1730. /*
  1731. * We repeat when a timestamp is encountered. It is possible
  1732. * to get multiple timestamps from an interrupt entering just
  1733. * as one timestamp is about to be written. The max times
  1734. * that this can happen is the number of nested interrupts we
  1735. * can have. Nesting 10 deep of interrupts is clearly
  1736. * an anomaly.
  1737. */
  1738. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1739. return NULL;
  1740. reader = rb_get_reader_page(cpu_buffer);
  1741. if (!reader)
  1742. return NULL;
  1743. event = rb_reader_event(cpu_buffer);
  1744. switch (event->type) {
  1745. case RINGBUF_TYPE_PADDING:
  1746. if (rb_null_event(event))
  1747. RB_WARN_ON(cpu_buffer, 1);
  1748. /*
  1749. * Because the writer could be discarding every
  1750. * event it creates (which would probably be bad)
  1751. * if we were to go back to "again" then we may never
  1752. * catch up, and will trigger the warn on, or lock
  1753. * the box. Return the padding, and we will release
  1754. * the current locks, and try again.
  1755. */
  1756. rb_advance_reader(cpu_buffer);
  1757. return event;
  1758. case RINGBUF_TYPE_TIME_EXTEND:
  1759. /* Internal data, OK to advance */
  1760. rb_advance_reader(cpu_buffer);
  1761. goto again;
  1762. case RINGBUF_TYPE_TIME_STAMP:
  1763. /* FIXME: not implemented */
  1764. rb_advance_reader(cpu_buffer);
  1765. goto again;
  1766. case RINGBUF_TYPE_DATA:
  1767. if (ts) {
  1768. *ts = cpu_buffer->read_stamp + event->time_delta;
  1769. ring_buffer_normalize_time_stamp(buffer,
  1770. cpu_buffer->cpu, ts);
  1771. }
  1772. return event;
  1773. default:
  1774. BUG();
  1775. }
  1776. return NULL;
  1777. }
  1778. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  1779. static struct ring_buffer_event *
  1780. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  1781. {
  1782. struct ring_buffer *buffer;
  1783. struct ring_buffer_per_cpu *cpu_buffer;
  1784. struct ring_buffer_event *event;
  1785. int nr_loops = 0;
  1786. if (ring_buffer_iter_empty(iter))
  1787. return NULL;
  1788. cpu_buffer = iter->cpu_buffer;
  1789. buffer = cpu_buffer->buffer;
  1790. again:
  1791. /*
  1792. * We repeat when a timestamp is encountered. It is possible
  1793. * to get multiple timestamps from an interrupt entering just
  1794. * as one timestamp is about to be written. The max times
  1795. * that this can happen is the number of nested interrupts we
  1796. * can have. Nesting 10 deep of interrupts is clearly
  1797. * an anomaly.
  1798. */
  1799. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1800. return NULL;
  1801. if (rb_per_cpu_empty(cpu_buffer))
  1802. return NULL;
  1803. event = rb_iter_head_event(iter);
  1804. switch (event->type) {
  1805. case RINGBUF_TYPE_PADDING:
  1806. if (rb_null_event(event)) {
  1807. rb_inc_iter(iter);
  1808. goto again;
  1809. }
  1810. rb_advance_iter(iter);
  1811. return event;
  1812. case RINGBUF_TYPE_TIME_EXTEND:
  1813. /* Internal data, OK to advance */
  1814. rb_advance_iter(iter);
  1815. goto again;
  1816. case RINGBUF_TYPE_TIME_STAMP:
  1817. /* FIXME: not implemented */
  1818. rb_advance_iter(iter);
  1819. goto again;
  1820. case RINGBUF_TYPE_DATA:
  1821. if (ts) {
  1822. *ts = iter->read_stamp + event->time_delta;
  1823. ring_buffer_normalize_time_stamp(buffer,
  1824. cpu_buffer->cpu, ts);
  1825. }
  1826. return event;
  1827. default:
  1828. BUG();
  1829. }
  1830. return NULL;
  1831. }
  1832. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  1833. /**
  1834. * ring_buffer_peek - peek at the next event to be read
  1835. * @buffer: The ring buffer to read
  1836. * @cpu: The cpu to peak at
  1837. * @ts: The timestamp counter of this event.
  1838. *
  1839. * This will return the event that will be read next, but does
  1840. * not consume the data.
  1841. */
  1842. struct ring_buffer_event *
  1843. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1844. {
  1845. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  1846. struct ring_buffer_event *event;
  1847. unsigned long flags;
  1848. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1849. return NULL;
  1850. again:
  1851. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1852. event = rb_buffer_peek(buffer, cpu, ts);
  1853. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1854. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1855. cpu_relax();
  1856. goto again;
  1857. }
  1858. return event;
  1859. }
  1860. /**
  1861. * ring_buffer_iter_peek - peek at the next event to be read
  1862. * @iter: The ring buffer iterator
  1863. * @ts: The timestamp counter of this event.
  1864. *
  1865. * This will return the event that will be read next, but does
  1866. * not increment the iterator.
  1867. */
  1868. struct ring_buffer_event *
  1869. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  1870. {
  1871. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1872. struct ring_buffer_event *event;
  1873. unsigned long flags;
  1874. again:
  1875. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1876. event = rb_iter_peek(iter, ts);
  1877. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1878. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1879. cpu_relax();
  1880. goto again;
  1881. }
  1882. return event;
  1883. }
  1884. /**
  1885. * ring_buffer_consume - return an event and consume it
  1886. * @buffer: The ring buffer to get the next event from
  1887. *
  1888. * Returns the next event in the ring buffer, and that event is consumed.
  1889. * Meaning, that sequential reads will keep returning a different event,
  1890. * and eventually empty the ring buffer if the producer is slower.
  1891. */
  1892. struct ring_buffer_event *
  1893. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  1894. {
  1895. struct ring_buffer_per_cpu *cpu_buffer;
  1896. struct ring_buffer_event *event = NULL;
  1897. unsigned long flags;
  1898. again:
  1899. /* might be called in atomic */
  1900. preempt_disable();
  1901. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1902. goto out;
  1903. cpu_buffer = buffer->buffers[cpu];
  1904. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1905. event = rb_buffer_peek(buffer, cpu, ts);
  1906. if (!event)
  1907. goto out_unlock;
  1908. rb_advance_reader(cpu_buffer);
  1909. out_unlock:
  1910. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1911. out:
  1912. preempt_enable();
  1913. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1914. cpu_relax();
  1915. goto again;
  1916. }
  1917. return event;
  1918. }
  1919. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  1920. /**
  1921. * ring_buffer_read_start - start a non consuming read of the buffer
  1922. * @buffer: The ring buffer to read from
  1923. * @cpu: The cpu buffer to iterate over
  1924. *
  1925. * This starts up an iteration through the buffer. It also disables
  1926. * the recording to the buffer until the reading is finished.
  1927. * This prevents the reading from being corrupted. This is not
  1928. * a consuming read, so a producer is not expected.
  1929. *
  1930. * Must be paired with ring_buffer_finish.
  1931. */
  1932. struct ring_buffer_iter *
  1933. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  1934. {
  1935. struct ring_buffer_per_cpu *cpu_buffer;
  1936. struct ring_buffer_iter *iter;
  1937. unsigned long flags;
  1938. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1939. return NULL;
  1940. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  1941. if (!iter)
  1942. return NULL;
  1943. cpu_buffer = buffer->buffers[cpu];
  1944. iter->cpu_buffer = cpu_buffer;
  1945. atomic_inc(&cpu_buffer->record_disabled);
  1946. synchronize_sched();
  1947. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1948. __raw_spin_lock(&cpu_buffer->lock);
  1949. rb_iter_reset(iter);
  1950. __raw_spin_unlock(&cpu_buffer->lock);
  1951. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1952. return iter;
  1953. }
  1954. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  1955. /**
  1956. * ring_buffer_finish - finish reading the iterator of the buffer
  1957. * @iter: The iterator retrieved by ring_buffer_start
  1958. *
  1959. * This re-enables the recording to the buffer, and frees the
  1960. * iterator.
  1961. */
  1962. void
  1963. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  1964. {
  1965. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1966. atomic_dec(&cpu_buffer->record_disabled);
  1967. kfree(iter);
  1968. }
  1969. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  1970. /**
  1971. * ring_buffer_read - read the next item in the ring buffer by the iterator
  1972. * @iter: The ring buffer iterator
  1973. * @ts: The time stamp of the event read.
  1974. *
  1975. * This reads the next event in the ring buffer and increments the iterator.
  1976. */
  1977. struct ring_buffer_event *
  1978. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  1979. {
  1980. struct ring_buffer_event *event;
  1981. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1982. unsigned long flags;
  1983. again:
  1984. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1985. event = rb_iter_peek(iter, ts);
  1986. if (!event)
  1987. goto out;
  1988. rb_advance_iter(iter);
  1989. out:
  1990. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1991. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1992. cpu_relax();
  1993. goto again;
  1994. }
  1995. return event;
  1996. }
  1997. EXPORT_SYMBOL_GPL(ring_buffer_read);
  1998. /**
  1999. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2000. * @buffer: The ring buffer.
  2001. */
  2002. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2003. {
  2004. return BUF_PAGE_SIZE * buffer->pages;
  2005. }
  2006. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2007. static void
  2008. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2009. {
  2010. cpu_buffer->head_page
  2011. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  2012. local_set(&cpu_buffer->head_page->write, 0);
  2013. local_set(&cpu_buffer->head_page->page->commit, 0);
  2014. cpu_buffer->head_page->read = 0;
  2015. cpu_buffer->tail_page = cpu_buffer->head_page;
  2016. cpu_buffer->commit_page = cpu_buffer->head_page;
  2017. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2018. local_set(&cpu_buffer->reader_page->write, 0);
  2019. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2020. cpu_buffer->reader_page->read = 0;
  2021. cpu_buffer->overrun = 0;
  2022. cpu_buffer->entries = 0;
  2023. cpu_buffer->write_stamp = 0;
  2024. cpu_buffer->read_stamp = 0;
  2025. }
  2026. /**
  2027. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2028. * @buffer: The ring buffer to reset a per cpu buffer of
  2029. * @cpu: The CPU buffer to be reset
  2030. */
  2031. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2032. {
  2033. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2034. unsigned long flags;
  2035. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2036. return;
  2037. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2038. __raw_spin_lock(&cpu_buffer->lock);
  2039. rb_reset_cpu(cpu_buffer);
  2040. __raw_spin_unlock(&cpu_buffer->lock);
  2041. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2042. }
  2043. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2044. /**
  2045. * ring_buffer_reset - reset a ring buffer
  2046. * @buffer: The ring buffer to reset all cpu buffers
  2047. */
  2048. void ring_buffer_reset(struct ring_buffer *buffer)
  2049. {
  2050. int cpu;
  2051. for_each_buffer_cpu(buffer, cpu)
  2052. ring_buffer_reset_cpu(buffer, cpu);
  2053. }
  2054. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2055. /**
  2056. * rind_buffer_empty - is the ring buffer empty?
  2057. * @buffer: The ring buffer to test
  2058. */
  2059. int ring_buffer_empty(struct ring_buffer *buffer)
  2060. {
  2061. struct ring_buffer_per_cpu *cpu_buffer;
  2062. int cpu;
  2063. /* yes this is racy, but if you don't like the race, lock the buffer */
  2064. for_each_buffer_cpu(buffer, cpu) {
  2065. cpu_buffer = buffer->buffers[cpu];
  2066. if (!rb_per_cpu_empty(cpu_buffer))
  2067. return 0;
  2068. }
  2069. return 1;
  2070. }
  2071. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2072. /**
  2073. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2074. * @buffer: The ring buffer
  2075. * @cpu: The CPU buffer to test
  2076. */
  2077. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2078. {
  2079. struct ring_buffer_per_cpu *cpu_buffer;
  2080. int ret;
  2081. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2082. return 1;
  2083. cpu_buffer = buffer->buffers[cpu];
  2084. ret = rb_per_cpu_empty(cpu_buffer);
  2085. return ret;
  2086. }
  2087. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2088. /**
  2089. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2090. * @buffer_a: One buffer to swap with
  2091. * @buffer_b: The other buffer to swap with
  2092. *
  2093. * This function is useful for tracers that want to take a "snapshot"
  2094. * of a CPU buffer and has another back up buffer lying around.
  2095. * it is expected that the tracer handles the cpu buffer not being
  2096. * used at the moment.
  2097. */
  2098. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2099. struct ring_buffer *buffer_b, int cpu)
  2100. {
  2101. struct ring_buffer_per_cpu *cpu_buffer_a;
  2102. struct ring_buffer_per_cpu *cpu_buffer_b;
  2103. int ret = -EINVAL;
  2104. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2105. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2106. goto out;
  2107. /* At least make sure the two buffers are somewhat the same */
  2108. if (buffer_a->pages != buffer_b->pages)
  2109. goto out;
  2110. ret = -EAGAIN;
  2111. if (ring_buffer_flags != RB_BUFFERS_ON)
  2112. goto out;
  2113. if (atomic_read(&buffer_a->record_disabled))
  2114. goto out;
  2115. if (atomic_read(&buffer_b->record_disabled))
  2116. goto out;
  2117. cpu_buffer_a = buffer_a->buffers[cpu];
  2118. cpu_buffer_b = buffer_b->buffers[cpu];
  2119. if (atomic_read(&cpu_buffer_a->record_disabled))
  2120. goto out;
  2121. if (atomic_read(&cpu_buffer_b->record_disabled))
  2122. goto out;
  2123. /*
  2124. * We can't do a synchronize_sched here because this
  2125. * function can be called in atomic context.
  2126. * Normally this will be called from the same CPU as cpu.
  2127. * If not it's up to the caller to protect this.
  2128. */
  2129. atomic_inc(&cpu_buffer_a->record_disabled);
  2130. atomic_inc(&cpu_buffer_b->record_disabled);
  2131. buffer_a->buffers[cpu] = cpu_buffer_b;
  2132. buffer_b->buffers[cpu] = cpu_buffer_a;
  2133. cpu_buffer_b->buffer = buffer_a;
  2134. cpu_buffer_a->buffer = buffer_b;
  2135. atomic_dec(&cpu_buffer_a->record_disabled);
  2136. atomic_dec(&cpu_buffer_b->record_disabled);
  2137. ret = 0;
  2138. out:
  2139. return ret;
  2140. }
  2141. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2142. static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
  2143. struct buffer_data_page *bpage,
  2144. unsigned int offset)
  2145. {
  2146. struct ring_buffer_event *event;
  2147. unsigned long head;
  2148. __raw_spin_lock(&cpu_buffer->lock);
  2149. for (head = offset; head < local_read(&bpage->commit);
  2150. head += rb_event_length(event)) {
  2151. event = __rb_data_page_index(bpage, head);
  2152. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  2153. return;
  2154. /* Only count data entries */
  2155. if (event->type != RINGBUF_TYPE_DATA)
  2156. continue;
  2157. cpu_buffer->entries--;
  2158. }
  2159. __raw_spin_unlock(&cpu_buffer->lock);
  2160. }
  2161. /**
  2162. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2163. * @buffer: the buffer to allocate for.
  2164. *
  2165. * This function is used in conjunction with ring_buffer_read_page.
  2166. * When reading a full page from the ring buffer, these functions
  2167. * can be used to speed up the process. The calling function should
  2168. * allocate a few pages first with this function. Then when it
  2169. * needs to get pages from the ring buffer, it passes the result
  2170. * of this function into ring_buffer_read_page, which will swap
  2171. * the page that was allocated, with the read page of the buffer.
  2172. *
  2173. * Returns:
  2174. * The page allocated, or NULL on error.
  2175. */
  2176. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  2177. {
  2178. struct buffer_data_page *bpage;
  2179. unsigned long addr;
  2180. addr = __get_free_page(GFP_KERNEL);
  2181. if (!addr)
  2182. return NULL;
  2183. bpage = (void *)addr;
  2184. rb_init_page(bpage);
  2185. return bpage;
  2186. }
  2187. /**
  2188. * ring_buffer_free_read_page - free an allocated read page
  2189. * @buffer: the buffer the page was allocate for
  2190. * @data: the page to free
  2191. *
  2192. * Free a page allocated from ring_buffer_alloc_read_page.
  2193. */
  2194. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  2195. {
  2196. free_page((unsigned long)data);
  2197. }
  2198. /**
  2199. * ring_buffer_read_page - extract a page from the ring buffer
  2200. * @buffer: buffer to extract from
  2201. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  2202. * @len: amount to extract
  2203. * @cpu: the cpu of the buffer to extract
  2204. * @full: should the extraction only happen when the page is full.
  2205. *
  2206. * This function will pull out a page from the ring buffer and consume it.
  2207. * @data_page must be the address of the variable that was returned
  2208. * from ring_buffer_alloc_read_page. This is because the page might be used
  2209. * to swap with a page in the ring buffer.
  2210. *
  2211. * for example:
  2212. * rpage = ring_buffer_alloc_read_page(buffer);
  2213. * if (!rpage)
  2214. * return error;
  2215. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  2216. * if (ret >= 0)
  2217. * process_page(rpage, ret);
  2218. *
  2219. * When @full is set, the function will not return true unless
  2220. * the writer is off the reader page.
  2221. *
  2222. * Note: it is up to the calling functions to handle sleeps and wakeups.
  2223. * The ring buffer can be used anywhere in the kernel and can not
  2224. * blindly call wake_up. The layer that uses the ring buffer must be
  2225. * responsible for that.
  2226. *
  2227. * Returns:
  2228. * >=0 if data has been transferred, returns the offset of consumed data.
  2229. * <0 if no data has been transferred.
  2230. */
  2231. int ring_buffer_read_page(struct ring_buffer *buffer,
  2232. void **data_page, size_t len, int cpu, int full)
  2233. {
  2234. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2235. struct ring_buffer_event *event;
  2236. struct buffer_data_page *bpage;
  2237. struct buffer_page *reader;
  2238. unsigned long flags;
  2239. unsigned int commit;
  2240. unsigned int read;
  2241. u64 save_timestamp;
  2242. int ret = -1;
  2243. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2244. goto out;
  2245. /*
  2246. * If len is not big enough to hold the page header, then
  2247. * we can not copy anything.
  2248. */
  2249. if (len <= BUF_PAGE_HDR_SIZE)
  2250. goto out;
  2251. len -= BUF_PAGE_HDR_SIZE;
  2252. if (!data_page)
  2253. goto out;
  2254. bpage = *data_page;
  2255. if (!bpage)
  2256. goto out;
  2257. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2258. reader = rb_get_reader_page(cpu_buffer);
  2259. if (!reader)
  2260. goto out_unlock;
  2261. event = rb_reader_event(cpu_buffer);
  2262. read = reader->read;
  2263. commit = rb_page_commit(reader);
  2264. /*
  2265. * If this page has been partially read or
  2266. * if len is not big enough to read the rest of the page or
  2267. * a writer is still on the page, then
  2268. * we must copy the data from the page to the buffer.
  2269. * Otherwise, we can simply swap the page with the one passed in.
  2270. */
  2271. if (read || (len < (commit - read)) ||
  2272. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  2273. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  2274. unsigned int rpos = read;
  2275. unsigned int pos = 0;
  2276. unsigned int size;
  2277. if (full)
  2278. goto out_unlock;
  2279. if (len > (commit - read))
  2280. len = (commit - read);
  2281. size = rb_event_length(event);
  2282. if (len < size)
  2283. goto out_unlock;
  2284. /* save the current timestamp, since the user will need it */
  2285. save_timestamp = cpu_buffer->read_stamp;
  2286. /* Need to copy one event at a time */
  2287. do {
  2288. memcpy(bpage->data + pos, rpage->data + rpos, size);
  2289. len -= size;
  2290. rb_advance_reader(cpu_buffer);
  2291. rpos = reader->read;
  2292. pos += size;
  2293. event = rb_reader_event(cpu_buffer);
  2294. size = rb_event_length(event);
  2295. } while (len > size);
  2296. /* update bpage */
  2297. local_set(&bpage->commit, pos);
  2298. bpage->time_stamp = save_timestamp;
  2299. /* we copied everything to the beginning */
  2300. read = 0;
  2301. } else {
  2302. /* swap the pages */
  2303. rb_init_page(bpage);
  2304. bpage = reader->page;
  2305. reader->page = *data_page;
  2306. local_set(&reader->write, 0);
  2307. reader->read = 0;
  2308. *data_page = bpage;
  2309. /* update the entry counter */
  2310. rb_remove_entries(cpu_buffer, bpage, read);
  2311. }
  2312. ret = read;
  2313. out_unlock:
  2314. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2315. out:
  2316. return ret;
  2317. }
  2318. static ssize_t
  2319. rb_simple_read(struct file *filp, char __user *ubuf,
  2320. size_t cnt, loff_t *ppos)
  2321. {
  2322. unsigned long *p = filp->private_data;
  2323. char buf[64];
  2324. int r;
  2325. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  2326. r = sprintf(buf, "permanently disabled\n");
  2327. else
  2328. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  2329. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2330. }
  2331. static ssize_t
  2332. rb_simple_write(struct file *filp, const char __user *ubuf,
  2333. size_t cnt, loff_t *ppos)
  2334. {
  2335. unsigned long *p = filp->private_data;
  2336. char buf[64];
  2337. unsigned long val;
  2338. int ret;
  2339. if (cnt >= sizeof(buf))
  2340. return -EINVAL;
  2341. if (copy_from_user(&buf, ubuf, cnt))
  2342. return -EFAULT;
  2343. buf[cnt] = 0;
  2344. ret = strict_strtoul(buf, 10, &val);
  2345. if (ret < 0)
  2346. return ret;
  2347. if (val)
  2348. set_bit(RB_BUFFERS_ON_BIT, p);
  2349. else
  2350. clear_bit(RB_BUFFERS_ON_BIT, p);
  2351. (*ppos)++;
  2352. return cnt;
  2353. }
  2354. static const struct file_operations rb_simple_fops = {
  2355. .open = tracing_open_generic,
  2356. .read = rb_simple_read,
  2357. .write = rb_simple_write,
  2358. };
  2359. static __init int rb_init_debugfs(void)
  2360. {
  2361. struct dentry *d_tracer;
  2362. struct dentry *entry;
  2363. d_tracer = tracing_init_dentry();
  2364. entry = debugfs_create_file("tracing_on", 0644, d_tracer,
  2365. &ring_buffer_flags, &rb_simple_fops);
  2366. if (!entry)
  2367. pr_warning("Could not create debugfs 'tracing_on' entry\n");
  2368. return 0;
  2369. }
  2370. fs_initcall(rb_init_debugfs);
  2371. #ifdef CONFIG_HOTPLUG_CPU
  2372. static int rb_cpu_notify(struct notifier_block *self,
  2373. unsigned long action, void *hcpu)
  2374. {
  2375. struct ring_buffer *buffer =
  2376. container_of(self, struct ring_buffer, cpu_notify);
  2377. long cpu = (long)hcpu;
  2378. switch (action) {
  2379. case CPU_UP_PREPARE:
  2380. case CPU_UP_PREPARE_FROZEN:
  2381. if (cpu_isset(cpu, *buffer->cpumask))
  2382. return NOTIFY_OK;
  2383. buffer->buffers[cpu] =
  2384. rb_allocate_cpu_buffer(buffer, cpu);
  2385. if (!buffer->buffers[cpu]) {
  2386. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  2387. cpu);
  2388. return NOTIFY_OK;
  2389. }
  2390. smp_wmb();
  2391. cpu_set(cpu, *buffer->cpumask);
  2392. break;
  2393. case CPU_DOWN_PREPARE:
  2394. case CPU_DOWN_PREPARE_FROZEN:
  2395. /*
  2396. * Do nothing.
  2397. * If we were to free the buffer, then the user would
  2398. * lose any trace that was in the buffer.
  2399. */
  2400. break;
  2401. default:
  2402. break;
  2403. }
  2404. return NOTIFY_OK;
  2405. }
  2406. #endif