ring_buffer.c 67 KB

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