13 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <linux/hash.h>
19 #include <linux/list.h>
23 #include <asm/local.h>
160 #define RB_BUFFER_OFF (1 << 20)
162 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
175 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
176 #define RB_ALIGNMENT 4U
177 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
178 #define RB_EVNT_MIN_SIZE 8U
180 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
181 # define RB_FORCE_8BYTE_ALIGNMENT 0
182 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
184 # define RB_FORCE_8BYTE_ALIGNMENT 1
185 # define RB_ARCH_ALIGNMENT 8U
189 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
196 #define skip_time_extend(event) \
197 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
208 event->time_delta = 0;
219 length =
event->array[0];
228 static inline unsigned
233 if (rb_null_event(event))
245 return rb_event_data_length(event);
257 static inline unsigned
267 return len + rb_event_length(event);
287 length = rb_event_length(event);
292 length -=
sizeof(
event->array[0]);
306 return (
void *)&
event->array[0];
308 return (
void *)&
event->array[1];
317 return rb_event_data(event);
321 #define for_each_buffer_cpu(buffer, cpu) \
322 for_each_cpu(cpu, buffer->cpumask)
325 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
326 #define TS_DELTA_TEST (~TS_MASK)
329 #define RB_MISSED_EVENTS (1 << 31)
331 #define RB_MISSED_STORED (1 << 30)
368 #define RB_WRITE_MASK 0xfffff
369 #define RB_WRITE_INTCNT (1 << 20)
392 static void free_buffer_page(
struct buffer_page *bpage)
401 static inline int test_time_stamp(
u64 delta)
408 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
411 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
419 "offset:0;\tsize:%u;\tsigned:%u;\n",
424 "offset:%u;\tsize:%u;\tsigned:%u;\n",
426 (
unsigned int)
sizeof(field.
commit),
430 "offset:%u;\tsize:%u;\tsigned:%u;\n",
436 "offset:%u;\tsize:%u;\tsigned:%u;\n",
492 #ifdef CONFIG_HOTPLUG_CPU
508 #define RB_WARN_ON(b, cond) \
510 int _____ret = unlikely(cond); \
512 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
513 struct ring_buffer_per_cpu *__b = \
515 atomic_inc(&__b->buffer->record_disabled); \
517 atomic_inc(&b->record_disabled); \
524 #define DEBUG_SHIFT 0
537 time = rb_time_stamp(buffer);
621 #define RB_PAGE_NORMAL 0UL
622 #define RB_PAGE_HEAD 1UL
623 #define RB_PAGE_UPDATE 2UL
626 #define RB_FLAG_MASK 3UL
629 #define RB_PAGE_MOVED 4UL
636 unsigned long val = (
unsigned long)list;
670 static int rb_is_reader_page(
struct buffer_page *page)
674 return rb_list_head(list->
next) != &page->
list;
685 ptr = (
unsigned long *)&list->
next;
704 rb_set_list_to_head(cpu_buffer, head->
list.prev);
707 static void rb_list_head_clear(
struct list_head *list)
709 unsigned long *
ptr = (
unsigned long *)&list->
next;
723 rb_list_head_clear(cpu_buffer->
pages);
726 rb_list_head_clear(hd);
735 unsigned long val = (
unsigned long)&head->list;
757 return rb_head_page_set(cpu_buffer, head, prev,
766 return rb_head_page_set(cpu_buffer, head, prev,
775 return rb_head_page_set(cpu_buffer, head, prev,
782 struct list_head *
p = rb_list_head((*bpage)->list.next);
799 list = cpu_buffer->
pages;
800 if (
RB_WARN_ON(cpu_buffer, rb_list_head(list->
prev->next) != list))
810 for (i = 0; i < 3; i++) {
812 if (rb_is_head_page(cpu_buffer, page, page->
list.prev)) {
816 rb_inc_page(cpu_buffer, &page);
817 }
while (page != head);
825 static int rb_head_page_replace(
struct buffer_page *old,
828 unsigned long *
ptr = (
unsigned long *)&old->
list.prev->next;
835 ret =
cmpxchg(ptr, val, (
unsigned long)&
new->list);
850 unsigned long old_entries;
851 unsigned long old_write;
877 if (tail_page == cpu_buffer->
tail_page) {
903 tail_page, next_page);
905 if (old_tail == tail_page)
915 unsigned long val = (
unsigned long)bpage;
950 rb_set_head_page(cpu_buffer);
952 rb_head_page_deactivate(cpu_buffer);
959 if (rb_check_list(cpu_buffer, head))
964 bpage->
list.next->prev != &bpage->
list))
967 bpage->
list.prev->next != &bpage->
list))
969 if (rb_check_list(cpu_buffer, &bpage->
list))
973 rb_head_page_activate(cpu_buffer);
983 for (i = 0; i < nr_pages; i++) {
996 list_add(&bpage->
list, pages);
1003 rb_init_page(bpage->
page);
1010 list_del_init(&bpage->
list);
1011 free_buffer_page(bpage);
1024 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->
cpu))
1037 rb_check_pages(cpu_buffer);
1066 goto fail_free_buffer;
1068 rb_check_bpage(cpu_buffer, bpage);
1073 goto fail_free_reader;
1075 rb_init_page(bpage->
page);
1080 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1082 goto fail_free_reader;
1088 rb_head_page_activate(cpu_buffer);
1107 rb_head_page_deactivate(cpu_buffer);
1111 list_del_init(&bpage->
list);
1112 free_buffer_page(bpage);
1115 free_buffer_page(bpage);
1121 #ifdef CONFIG_HOTPLUG_CPU
1123 unsigned long action,
void *hcpu);
1150 goto fail_free_buffer;
1166 #ifdef CONFIG_HOTPLUG_CPU
1178 goto fail_free_cpumask;
1182 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1184 goto fail_free_buffers;
1187 #ifdef CONFIG_HOTPLUG_CPU
1188 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1189 buffer->cpu_notify.priority = 0;
1190 register_cpu_notifier(&buffer->cpu_notify);
1201 rb_free_cpu_buffer(buffer->
buffers[cpu]);
1206 free_cpumask_var(buffer->
cpumask);
1226 #ifdef CONFIG_HOTPLUG_CPU
1227 unregister_cpu_notifier(&buffer->cpu_notify);
1231 rb_free_cpu_buffer(buffer->
buffers[cpu]);
1236 free_cpumask_var(buffer->
cpumask);
1250 static inline unsigned long rb_page_entries(
struct buffer_page *bpage)
1255 static inline unsigned long rb_page_write(
struct buffer_page *bpage)
1263 struct list_head *tail_page, *to_remove, *next_page;
1264 struct buffer_page *to_remove_page, *tmp_iter_page;
1266 unsigned int nr_removed;
1267 unsigned long head_bit;
1283 tail_page = &cpu_buffer->
tail_page->list;
1290 tail_page = rb_list_head(tail_page->
next);
1291 to_remove = tail_page;
1297 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1298 to_remove = rb_list_head(to_remove)->next;
1302 next_page = rb_list_head(to_remove)->next;
1309 tail_page->
next = (
struct list_head *)((
unsigned long)next_page |
1311 next_page = rb_list_head(next_page);
1312 next_page->
prev = tail_page;
1315 cpu_buffer->
pages = next_page;
1326 cpu_buffer->
read = 0;
1337 tmp_iter_page = first_page;
1340 to_remove_page = tmp_iter_page;
1341 rb_inc_page(cpu_buffer, &tmp_iter_page);
1344 page_entries = rb_page_entries(to_remove_page);
1360 free_buffer_page(to_remove_page);
1363 }
while (to_remove_page != last_page);
1367 return nr_removed == 0;
1395 struct list_head *last_page, *first_page;
1398 head_page = &rb_set_head_page(cpu_buffer)->list;
1401 prev_page = head_page->
prev;
1403 first_page = pages->
next;
1404 last_page = pages->
prev;
1406 head_page_with_bit = (
struct list_head *)
1409 last_page->
next = head_page_with_bit;
1410 first_page->
prev = prev_page;
1412 r =
cmpxchg(&prev_page->
next, head_page_with_bit, first_page);
1414 if (r == head_page_with_bit) {
1420 head_page->
prev = last_page;
1427 INIT_LIST_HEAD(pages);
1440 list_del_init(&bpage->
list);
1441 free_buffer_page(bpage);
1452 success = rb_insert_pages(cpu_buffer);
1454 success = rb_remove_pages(cpu_buffer,
1465 rb_update_pages(cpu_buffer);
1556 rb_update_pages(cpu_buffer);
1578 if (nr_pages == cpu_buffer->
nr_pages)
1599 rb_update_pages(cpu_buffer);
1624 rb_check_pages(cpu_buffer);
1644 list_del_init(&bpage->
list);
1645 free_buffer_page(bpage);
1664 static inline void *
1670 static inline void *__rb_page_index(
struct buffer_page *bpage,
unsigned index)
1688 static inline unsigned rb_page_commit(
struct buffer_page *bpage)
1694 static inline unsigned rb_page_size(
struct buffer_page *bpage)
1696 return rb_page_commit(bpage);
1699 static inline unsigned
1705 static inline unsigned
1708 unsigned long addr = (
unsigned long)event;
1717 unsigned long addr = (
unsigned long)event;
1718 unsigned long index;
1720 index = rb_event_index(event);
1723 return cpu_buffer->
commit_page->page == (
void *)addr &&
1724 rb_commit_index(cpu_buffer) ==
index;
1741 max_count = cpu_buffer->
nr_pages * 100;
1747 rb_is_reader_page(cpu_buffer->
tail_page)))
1751 rb_inc_page(cpu_buffer, &cpu_buffer->
commit_page);
1757 while (rb_commit_index(cpu_buffer) !=
1797 iter->
head_page = rb_set_head_page(cpu_buffer);
1799 rb_inc_page(cpu_buffer, &iter->
head_page);
1812 if (rb_event_index(event)) {
1813 event->time_delta = delta &
TS_MASK;
1814 event->array[0] = delta >>
TS_SHIFT;
1817 event->time_delta = 0;
1818 event->array[0] = 0;
1838 int add_timestamp,
u64 delta)
1841 if (
unlikely(!rb_event_is_commit(cpu_buffer, event)))
1849 event = rb_add_time_stamp(event, delta);
1854 event->time_delta =
delta;
1857 event->type_len = 0;
1858 event->array[0] =
length;
1880 entries = rb_page_entries(next_page);
1887 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1958 new_head = next_page;
1959 rb_inc_page(cpu_buffer, &new_head);
1961 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1997 if (cpu_buffer->
tail_page != tail_page &&
1999 rb_head_page_set_normal(cpu_buffer, new_head,
2010 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2021 static unsigned rb_calculate_event_length(
unsigned length)
2030 length +=
sizeof(
event.array[0]);
2041 unsigned long tail,
unsigned long length)
2062 event = __rb_page_index(tail_page, tail);
2090 rb_event_set_padding(event);
2101 event->time_delta = 1;
2113 unsigned long length,
unsigned long tail,
2121 next_page = tail_page;
2123 rb_inc_page(cpu_buffer, &next_page);
2130 if (
unlikely(next_page == commit_page)) {
2149 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->
list)) {
2155 if (!rb_is_reader_page(cpu_buffer->
commit_page)) {
2163 ret = rb_handle_head_page(cpu_buffer,
2191 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2197 ts = rb_time_stamp(buffer);
2198 next_page->
page->time_stamp =
ts;
2203 rb_reset_tail(cpu_buffer, tail_page, tail, length);
2210 rb_reset_tail(cpu_buffer, tail_page, tail, length);
2217 unsigned long length,
u64 ts,
2218 u64 delta,
int add_timestamp)
2241 return rb_move_tail(cpu_buffer, length, tail,
2246 event = __rb_page_index(tail_page, tail);
2248 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
2257 tail_page->
page->time_stamp =
ts;
2269 unsigned long new_index, old_index;
2271 unsigned long index;
2274 new_index = rb_event_index(event);
2275 old_index = new_index + rb_event_ts_length(event);
2276 addr = (
unsigned long)event;
2281 if (bpage->
page == (
void *)addr && rb_page_write(bpage) == old_index) {
2282 unsigned long write_mask =
2284 unsigned long event_length = rb_event_length(event);
2291 old_index += write_mask;
2292 new_index += write_mask;
2294 if (index == old_index) {
2313 unsigned long commits;
2324 rb_set_commit_to_write(cpu_buffer);
2346 unsigned long length)
2354 rb_start_commit(cpu_buffer);
2356 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2371 length = rb_calculate_event_length(length);
2385 if (
RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
2388 ts = rb_time_stamp(cpu_buffer->
buffer);
2397 if (
unlikely(test_time_stamp(delta))) {
2398 int local_clock_stable = 1;
2399 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2400 local_clock_stable = sched_clock_stable;
2403 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2404 (
unsigned long long)delta,
2405 (
unsigned long long)ts,
2407 local_clock_stable ?
"" :
2408 "If you just came from a suspend/resume,\n"
2409 "please switch to the trace global clock:\n"
2410 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
2415 event = __rb_reserve_next(cpu_buffer, length, ts,
2416 delta, add_timestamp);
2426 rb_end_commit(cpu_buffer);
2430 #ifdef CONFIG_TRACING
2432 #define TRACE_RECURSIVE_DEPTH 16
2435 static noinline void trace_recursive_fail(
void)
2441 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2457 trace_recursive_fail();
2471 #define trace_recursive_lock() (0)
2472 #define trace_recursive_unlock() do { } while (0)
2523 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2548 if (rb_event_is_commit(cpu_buffer, event)) {
2553 if (!rb_event_index(event))
2557 delta =
event->array[0];
2559 delta +=
event->time_delta;
2570 rb_update_write_stamp(cpu_buffer, event);
2571 rb_end_commit(cpu_buffer);
2591 rb_commit(cpu_buffer, event);
2611 event->time_delta = 1;
2624 unsigned long addr = (
unsigned long)event;
2640 rb_inc_page(cpu_buffer, &bpage);
2643 if (bpage->
page == (
void *)addr) {
2647 rb_inc_page(cpu_buffer, &bpage);
2648 }
while (bpage != start);
2680 rb_event_discard(event);
2692 rb_decrement_entry(cpu_buffer, event);
2693 if (rb_try_to_discard(cpu_buffer, event))
2700 rb_update_write_stamp(cpu_buffer, event);
2702 rb_end_commit(cpu_buffer);
2725 unsigned long length,
2755 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2759 body = rb_event_data(event);
2761 memcpy(body, data, length);
2763 rb_commit(cpu_buffer, event);
2776 struct buffer_page *head = rb_set_head_page(cpu_buffer);
2783 return reader->
read == rb_page_commit(reader) &&
2784 (commit == reader ||
2786 head->
read == rb_page_commit(commit)));
2831 unsigned int new_rd;
2854 unsigned int new_rd;
2922 static inline unsigned long
2936 unsigned long flags;
2939 unsigned long ret = 0;
2953 bpage = rb_set_head_page(cpu_buffer);
2955 ret = bpage->
page->time_stamp;
2996 return rb_num_of_entries(cpu_buffer);
3051 unsigned long entries = 0;
3057 entries += rb_num_of_entries(cpu_buffer);
3074 unsigned long overruns = 0;
3093 iter->
head_page = rb_set_head_page(cpu_buffer);
3119 unsigned long flags;
3127 rb_iter_reset(iter);
3143 iter->
head == rb_commit_index(cpu_buffer);
3158 delta =
event->array[0];
3160 delta +=
event->time_delta;
3189 delta =
event->array[0];
3191 delta +=
event->time_delta;
3214 unsigned long flags;
3228 if (
RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
3236 if (cpu_buffer->
reader_page->read < rb_page_size(reader))
3241 cpu_buffer->
reader_page->read > rb_page_size(reader)))
3250 if (rb_num_of_entries(cpu_buffer) == 0)
3265 reader = rb_set_head_page(cpu_buffer);
3279 rb_set_list_to_head(cpu_buffer, &cpu_buffer->
reader_page->list);
3304 ret = rb_head_page_replace(reader, cpu_buffer->
reader_page);
3317 rb_list_head(reader->
list.next)->prev = &cpu_buffer->
reader_page->list;
3318 rb_inc_page(cpu_buffer, &cpu_buffer->
head_page);
3322 rb_reset_reader_page(cpu_buffer);
3344 reader = rb_get_reader_page(cpu_buffer);
3350 event = rb_reader_event(cpu_buffer);
3355 rb_update_read_stamp(cpu_buffer, event);
3357 length = rb_event_length(event);
3380 event = rb_iter_head_event(iter);
3382 length = rb_event_length(event);
3390 (iter->
head + length > rb_commit_index(cpu_buffer))))
3393 rb_update_iter_read_stamp(iter, event);
3400 rb_advance_iter(iter);
3410 unsigned long *lost_events)
3426 reader = rb_get_reader_page(cpu_buffer);
3430 event = rb_reader_event(cpu_buffer);
3434 if (rb_null_event(event))
3448 rb_advance_reader(cpu_buffer);
3453 rb_advance_reader(cpu_buffer);
3458 *ts = cpu_buffer->
read_stamp +
event->time_delta;
3460 cpu_buffer->
cpu, ts);
3463 *lost_events = rb_lost_events(cpu_buffer);
3483 buffer = cpu_buffer->
buffer;
3492 rb_iter_reset(iter);
3507 if (rb_per_cpu_empty(cpu_buffer))
3515 event = rb_iter_head_event(iter);
3519 if (rb_null_event(event)) {
3523 rb_advance_iter(iter);
3528 rb_advance_iter(iter);
3533 rb_advance_iter(iter);
3540 cpu_buffer->
cpu, ts);
3552 static inline int rb_ok_to_lock(
void)
3579 unsigned long *lost_events)
3583 unsigned long flags;
3589 dolock = rb_ok_to_lock();
3594 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3596 rb_advance_reader(cpu_buffer);
3620 unsigned long flags;
3624 event = rb_iter_peek(iter, ts);
3646 unsigned long *lost_events)
3650 unsigned long flags;
3653 dolock = rb_ok_to_lock();
3667 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3670 rb_advance_reader(cpu_buffer);
3760 unsigned long flags;
3769 rb_iter_reset(iter);
3786 unsigned long flags;
3795 rb_check_pages(cpu_buffer);
3816 unsigned long flags;
3820 event = rb_iter_peek(iter, ts);
3827 rb_advance_iter(iter);
3857 rb_head_page_deactivate(cpu_buffer);
3883 cpu_buffer->
read = 0;
3892 rb_head_page_activate(cpu_buffer);
3903 unsigned long flags;
3921 rb_reset_cpu(cpu_buffer);
3953 unsigned long flags;
3958 dolock = rb_ok_to_lock();
3966 ret = rb_per_cpu_empty(cpu_buffer);
3987 unsigned long flags;
3994 dolock = rb_ok_to_lock();
4000 ret = rb_per_cpu_empty(cpu_buffer);
4009 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4020 int ring_buffer_swap_cpu(
struct ring_buffer *buffer_a,
4073 cpu_buffer_b->
buffer = buffer_a;
4074 cpu_buffer_a->
buffer = buffer_b;
4114 rb_init_page(bpage);
4167 void **data_page,
size_t len,
int cpu,
int full)
4173 unsigned long missed_events;
4174 unsigned long flags;
4175 unsigned int commit;
4201 reader = rb_get_reader_page(cpu_buffer);
4205 event = rb_reader_event(cpu_buffer);
4207 read = reader->
read;
4208 commit = rb_page_commit(reader);
4220 if (read || (len < (commit - read)) ||
4224 unsigned int pos = 0;
4230 if (len > (commit - read))
4231 len = (commit -
read);
4234 size = rb_event_ts_length(event);
4250 size = rb_event_length(event);
4255 rb_advance_reader(cpu_buffer);
4256 rpos = reader->
read;
4262 event = rb_reader_event(cpu_buffer);
4264 size = rb_event_ts_length(event);
4265 }
while (len >= size);
4275 cpu_buffer->
read += rb_page_entries(reader);
4279 rb_init_page(bpage);
4280 bpage = reader->
page;
4281 reader->
page = *data_page;
4303 if (missed_events) {
4309 sizeof(missed_events));
4311 commit +=
sizeof(missed_events);
4330 #ifdef CONFIG_HOTPLUG_CPU
4332 unsigned long action,
void *hcpu)
4336 long cpu = (
long)hcpu;
4337 int cpu_i, nr_pages_same;
4352 nr_pages = buffer->
buffers[cpu_i]->nr_pages;
4353 if (nr_pages != buffer->
buffers[cpu_i]->nr_pages) {
4362 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
4364 WARN(1,
"failed to allocate ring buffer on CPU %ld\n",
4369 cpumask_set_cpu(cpu, buffer->
cpumask);