ring_buffer.c 69 KB

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