ring_buffer.c 69 KB

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