blob: b400e6429c02c1e6d450ff660ee4d90cef9ae9f4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef _KERNEL_EVENTS_INTERNAL_H
2#define _KERNEL_EVENTS_INTERNAL_H
3
4#include <linux/hardirq.h>
5
6/* Buffer handling */
7
8#define RING_BUFFER_WRITABLE 0x01
9
10struct ring_buffer {
11 atomic_t refcount;
12 struct rcu_head rcu_head;
13#ifdef CONFIG_PERF_USE_VMALLOC
14 struct work_struct work;
15 int page_order; /* allocation order */
16#endif
17 int nr_pages; /* nr of data pages */
18 int writable; /* are we writable */
19
20 atomic_t poll; /* POLL_ for wakeups */
21
22 local_t head; /* write position */
23 local_t nest; /* nested writers */
24 local_t events; /* event limit */
25 local_t wakeup; /* wakeup stamp */
26 local_t lost; /* nr records lost */
27
28 long watermark; /* wakeup watermark */
29 /* poll crap */
30 spinlock_t event_lock;
31 struct list_head event_list;
32
33 atomic_t mmap_count;
34 unsigned long mmap_locked;
35 struct user_struct *mmap_user;
36
37 struct perf_event_mmap_page *user_page;
38 void *data_pages[0];
39};
40
41extern void rb_free(struct ring_buffer *rb);
42extern struct ring_buffer *
43rb_alloc(int nr_pages, long watermark, int cpu, int flags);
44extern void perf_event_wakeup(struct perf_event *event);
45
46extern void
47perf_event_header__init_id(struct perf_event_header *header,
48 struct perf_sample_data *data,
49 struct perf_event *event);
50extern void
51perf_event__output_id_sample(struct perf_event *event,
52 struct perf_output_handle *handle,
53 struct perf_sample_data *sample);
54
55extern struct page *
56perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);
57
58#ifdef CONFIG_PERF_USE_VMALLOC
59/*
60 * Back perf_mmap() with vmalloc memory.
61 *
62 * Required for architectures that have d-cache aliasing issues.
63 */
64
65static inline int page_order(struct ring_buffer *rb)
66{
67 return rb->page_order;
68}
69
70#else
71
72static inline int page_order(struct ring_buffer *rb)
73{
74 return 0;
75}
76#endif
77
78static inline unsigned long perf_data_size(struct ring_buffer *rb)
79{
80 return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
81}
82
83static inline void
84__output_copy(struct perf_output_handle *handle,
85 const void *buf, unsigned int len)
86{
87 do {
88 unsigned long size = min_t(unsigned long, handle->size, len);
89
90 memcpy(handle->addr, buf, size);
91
92 len -= size;
93 handle->addr += size;
94 buf += size;
95 handle->size -= size;
96 if (!handle->size) {
97 struct ring_buffer *rb = handle->rb;
98
99 handle->page++;
100 handle->page &= rb->nr_pages - 1;
101 handle->addr = rb->data_pages[handle->page];
102 handle->size = PAGE_SIZE << page_order(rb);
103 }
104 } while (len);
105}
106
107/* Callchain handling */
108extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
109extern int get_callchain_buffers(void);
110extern void put_callchain_buffers(void);
111
112static inline int get_recursion_context(int *recursion)
113{
114 int rctx;
115
116 if (in_nmi())
117 rctx = 3;
118 else if (in_irq())
119 rctx = 2;
120 else if (in_softirq())
121 rctx = 1;
122 else
123 rctx = 0;
124
125 if (recursion[rctx])
126 return -1;
127
128 recursion[rctx]++;
129 barrier();
130
131 return rctx;
132}
133
134static inline void put_recursion_context(int *recursion, int rctx)
135{
136 barrier();
137 recursion[rctx]--;
138}
139
140#endif /* _KERNEL_EVENTS_INTERNAL_H */