perf: Add AUX record
[cascardo/linux.git] / kernel / events / internal.h
1 #ifndef _KERNEL_EVENTS_INTERNAL_H
2 #define _KERNEL_EVENTS_INTERNAL_H
3
4 #include <linux/hardirq.h>
5 #include <linux/uaccess.h>
6
7 /* Buffer handling */
8
9 #define RING_BUFFER_WRITABLE            0x01
10
11 struct ring_buffer {
12         atomic_t                        refcount;
13         struct rcu_head                 rcu_head;
14 #ifdef CONFIG_PERF_USE_VMALLOC
15         struct work_struct              work;
16         int                             page_order;     /* allocation order  */
17 #endif
18         int                             nr_pages;       /* nr of data pages  */
19         int                             overwrite;      /* can overwrite itself */
20
21         atomic_t                        poll;           /* POLL_ for wakeups */
22
23         local_t                         head;           /* write position    */
24         local_t                         nest;           /* nested writers    */
25         local_t                         events;         /* event limit       */
26         local_t                         wakeup;         /* wakeup stamp      */
27         local_t                         lost;           /* nr records lost   */
28
29         long                            watermark;      /* wakeup watermark  */
30         /* poll crap */
31         spinlock_t                      event_lock;
32         struct list_head                event_list;
33
34         atomic_t                        mmap_count;
35         unsigned long                   mmap_locked;
36         struct user_struct              *mmap_user;
37
38         /* AUX area */
39         unsigned long                   aux_pgoff;
40         int                             aux_nr_pages;
41         atomic_t                        aux_mmap_count;
42         unsigned long                   aux_mmap_locked;
43         void                            (*free_aux)(void *);
44         atomic_t                        aux_refcount;
45         void                            **aux_pages;
46         void                            *aux_priv;
47
48         struct perf_event_mmap_page     *user_page;
49         void                            *data_pages[0];
50 };
51
52 extern void rb_free(struct ring_buffer *rb);
53 extern struct ring_buffer *
54 rb_alloc(int nr_pages, long watermark, int cpu, int flags);
55 extern void perf_event_wakeup(struct perf_event *event);
56 extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
57                         pgoff_t pgoff, int nr_pages, int flags);
58 extern void rb_free_aux(struct ring_buffer *rb);
59
60 static inline bool rb_has_aux(struct ring_buffer *rb)
61 {
62         return !!rb->aux_nr_pages;
63 }
64
65 void perf_event_aux_event(struct perf_event *event, unsigned long head,
66                           unsigned long size, u64 flags);
67
68 extern void
69 perf_event_header__init_id(struct perf_event_header *header,
70                            struct perf_sample_data *data,
71                            struct perf_event *event);
72 extern void
73 perf_event__output_id_sample(struct perf_event *event,
74                              struct perf_output_handle *handle,
75                              struct perf_sample_data *sample);
76
77 extern struct page *
78 perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);
79
80 #ifdef CONFIG_PERF_USE_VMALLOC
81 /*
82  * Back perf_mmap() with vmalloc memory.
83  *
84  * Required for architectures that have d-cache aliasing issues.
85  */
86
87 static inline int page_order(struct ring_buffer *rb)
88 {
89         return rb->page_order;
90 }
91
92 #else
93
94 static inline int page_order(struct ring_buffer *rb)
95 {
96         return 0;
97 }
98 #endif
99
100 static inline unsigned long perf_data_size(struct ring_buffer *rb)
101 {
102         return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
103 }
104
105 static inline unsigned long perf_aux_size(struct ring_buffer *rb)
106 {
107         return rb->aux_nr_pages << PAGE_SHIFT;
108 }
109
110 #define DEFINE_OUTPUT_COPY(func_name, memcpy_func)                      \
111 static inline unsigned long                                             \
112 func_name(struct perf_output_handle *handle,                            \
113           const void *buf, unsigned long len)                           \
114 {                                                                       \
115         unsigned long size, written;                                    \
116                                                                         \
117         do {                                                            \
118                 size    = min(handle->size, len);                       \
119                 written = memcpy_func(handle->addr, buf, size);         \
120                 written = size - written;                               \
121                                                                         \
122                 len -= written;                                         \
123                 handle->addr += written;                                \
124                 buf += written;                                         \
125                 handle->size -= written;                                \
126                 if (!handle->size) {                                    \
127                         struct ring_buffer *rb = handle->rb;            \
128                                                                         \
129                         handle->page++;                                 \
130                         handle->page &= rb->nr_pages - 1;               \
131                         handle->addr = rb->data_pages[handle->page];    \
132                         handle->size = PAGE_SIZE << page_order(rb);     \
133                 }                                                       \
134         } while (len && written == size);                               \
135                                                                         \
136         return len;                                                     \
137 }
138
139 static inline unsigned long
140 memcpy_common(void *dst, const void *src, unsigned long n)
141 {
142         memcpy(dst, src, n);
143         return 0;
144 }
145
146 DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
147
148 static inline unsigned long
149 memcpy_skip(void *dst, const void *src, unsigned long n)
150 {
151         return 0;
152 }
153
154 DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
155
156 #ifndef arch_perf_out_copy_user
157 #define arch_perf_out_copy_user arch_perf_out_copy_user
158
159 static inline unsigned long
160 arch_perf_out_copy_user(void *dst, const void *src, unsigned long n)
161 {
162         unsigned long ret;
163
164         pagefault_disable();
165         ret = __copy_from_user_inatomic(dst, src, n);
166         pagefault_enable();
167
168         return ret;
169 }
170 #endif
171
172 DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
173
174 /* Callchain handling */
175 extern struct perf_callchain_entry *
176 perf_callchain(struct perf_event *event, struct pt_regs *regs);
177 extern int get_callchain_buffers(void);
178 extern void put_callchain_buffers(void);
179
180 static inline int get_recursion_context(int *recursion)
181 {
182         int rctx;
183
184         if (in_nmi())
185                 rctx = 3;
186         else if (in_irq())
187                 rctx = 2;
188         else if (in_softirq())
189                 rctx = 1;
190         else
191                 rctx = 0;
192
193         if (recursion[rctx])
194                 return -1;
195
196         recursion[rctx]++;
197         barrier();
198
199         return rctx;
200 }
201
202 static inline void put_recursion_context(int *recursion, int rctx)
203 {
204         barrier();
205         recursion[rctx]--;
206 }
207
208 #ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
209 static inline bool arch_perf_have_user_stack_dump(void)
210 {
211         return true;
212 }
213
214 #define perf_user_stack_pointer(regs) user_stack_pointer(regs)
215 #else
216 static inline bool arch_perf_have_user_stack_dump(void)
217 {
218         return false;
219 }
220
221 #define perf_user_stack_pointer(regs) 0
222 #endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */
223
224 #endif /* _KERNEL_EVENTS_INTERNAL_H */