ring_buffer.c 66 KB

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