ring_buffer.c 71 KB

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