Linux 4.15.6
[linux/fpc-iii.git] / kernel / events / internal.h
blob09b1537ae06cd4645458a12ea111b4cf813038da
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _KERNEL_EVENTS_INTERNAL_H
3 #define _KERNEL_EVENTS_INTERNAL_H
5 #include <linux/hardirq.h>
6 #include <linux/uaccess.h>
8 /* Buffer handling */
10 #define RING_BUFFER_WRITABLE 0x01
12 struct ring_buffer {
13 atomic_t refcount;
14 struct rcu_head rcu_head;
15 #ifdef CONFIG_PERF_USE_VMALLOC
16 struct work_struct work;
17 int page_order; /* allocation order */
18 #endif
19 int nr_pages; /* nr of data pages */
20 int overwrite; /* can overwrite itself */
21 int paused; /* can write into ring buffer */
23 atomic_t poll; /* POLL_ for wakeups */
25 local_t head; /* write position */
26 local_t nest; /* nested writers */
27 local_t events; /* event limit */
28 local_t wakeup; /* wakeup stamp */
29 local_t lost; /* nr records lost */
31 long watermark; /* wakeup watermark */
32 long aux_watermark;
33 /* poll crap */
34 spinlock_t event_lock;
35 struct list_head event_list;
37 atomic_t mmap_count;
38 unsigned long mmap_locked;
39 struct user_struct *mmap_user;
41 /* AUX area */
42 long aux_head;
43 local_t aux_nest;
44 long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */
45 unsigned long aux_pgoff;
46 int aux_nr_pages;
47 int aux_overwrite;
48 atomic_t aux_mmap_count;
49 unsigned long aux_mmap_locked;
50 void (*free_aux)(void *);
51 atomic_t aux_refcount;
52 void **aux_pages;
53 void *aux_priv;
55 struct perf_event_mmap_page *user_page;
56 void *data_pages[0];
59 extern void rb_free(struct ring_buffer *rb);
61 static inline void rb_free_rcu(struct rcu_head *rcu_head)
63 struct ring_buffer *rb;
65 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
66 rb_free(rb);
69 static inline void rb_toggle_paused(struct ring_buffer *rb, bool pause)
71 if (!pause && rb->nr_pages)
72 rb->paused = 0;
73 else
74 rb->paused = 1;
77 extern struct ring_buffer *
78 rb_alloc(int nr_pages, long watermark, int cpu, int flags);
79 extern void perf_event_wakeup(struct perf_event *event);
80 extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
81 pgoff_t pgoff, int nr_pages, long watermark, int flags);
82 extern void rb_free_aux(struct ring_buffer *rb);
83 extern struct ring_buffer *ring_buffer_get(struct perf_event *event);
84 extern void ring_buffer_put(struct ring_buffer *rb);
86 static inline bool rb_has_aux(struct ring_buffer *rb)
88 return !!rb->aux_nr_pages;
91 void perf_event_aux_event(struct perf_event *event, unsigned long head,
92 unsigned long size, u64 flags);
94 extern struct page *
95 perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);
97 #ifdef CONFIG_PERF_USE_VMALLOC
99 * Back perf_mmap() with vmalloc memory.
101 * Required for architectures that have d-cache aliasing issues.
104 static inline int page_order(struct ring_buffer *rb)
106 return rb->page_order;
109 #else
111 static inline int page_order(struct ring_buffer *rb)
113 return 0;
115 #endif
117 static inline unsigned long perf_data_size(struct ring_buffer *rb)
119 return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
122 static inline unsigned long perf_aux_size(struct ring_buffer *rb)
124 return rb->aux_nr_pages << PAGE_SHIFT;
127 #define __DEFINE_OUTPUT_COPY_BODY(advance_buf, memcpy_func, ...) \
129 unsigned long size, written; \
131 do { \
132 size = min(handle->size, len); \
133 written = memcpy_func(__VA_ARGS__); \
134 written = size - written; \
136 len -= written; \
137 handle->addr += written; \
138 if (advance_buf) \
139 buf += written; \
140 handle->size -= written; \
141 if (!handle->size) { \
142 struct ring_buffer *rb = handle->rb; \
144 handle->page++; \
145 handle->page &= rb->nr_pages - 1; \
146 handle->addr = rb->data_pages[handle->page]; \
147 handle->size = PAGE_SIZE << page_order(rb); \
149 } while (len && written == size); \
151 return len; \
154 #define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \
155 static inline unsigned long \
156 func_name(struct perf_output_handle *handle, \
157 const void *buf, unsigned long len) \
158 __DEFINE_OUTPUT_COPY_BODY(true, memcpy_func, handle->addr, buf, size)
160 static inline unsigned long
161 __output_custom(struct perf_output_handle *handle, perf_copy_f copy_func,
162 const void *buf, unsigned long len)
164 unsigned long orig_len = len;
165 __DEFINE_OUTPUT_COPY_BODY(false, copy_func, handle->addr, buf,
166 orig_len - len, size)
169 static inline unsigned long
170 memcpy_common(void *dst, const void *src, unsigned long n)
172 memcpy(dst, src, n);
173 return 0;
176 DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
178 static inline unsigned long
179 memcpy_skip(void *dst, const void *src, unsigned long n)
181 return 0;
184 DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
186 #ifndef arch_perf_out_copy_user
187 #define arch_perf_out_copy_user arch_perf_out_copy_user
189 static inline unsigned long
190 arch_perf_out_copy_user(void *dst, const void *src, unsigned long n)
192 unsigned long ret;
194 pagefault_disable();
195 ret = __copy_from_user_inatomic(dst, src, n);
196 pagefault_enable();
198 return ret;
200 #endif
202 DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
204 /* Callchain handling */
205 extern struct perf_callchain_entry *
206 perf_callchain(struct perf_event *event, struct pt_regs *regs);
208 static inline int get_recursion_context(int *recursion)
210 int rctx;
212 if (unlikely(in_nmi()))
213 rctx = 3;
214 else if (in_irq())
215 rctx = 2;
216 else if (in_softirq())
217 rctx = 1;
218 else
219 rctx = 0;
221 if (recursion[rctx])
222 return -1;
224 recursion[rctx]++;
225 barrier();
227 return rctx;
230 static inline void put_recursion_context(int *recursion, int rctx)
232 barrier();
233 recursion[rctx]--;
236 #ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
237 static inline bool arch_perf_have_user_stack_dump(void)
239 return true;
242 #define perf_user_stack_pointer(regs) user_stack_pointer(regs)
243 #else
244 static inline bool arch_perf_have_user_stack_dump(void)
246 return false;
249 #define perf_user_stack_pointer(regs) 0
250 #endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */
252 #endif /* _KERNEL_EVENTS_INTERNAL_H */