ring_buffer.c 68 KB

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