Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 2 | #ifndef _KERNEL_EVENTS_INTERNAL_H |
| 3 | #define _KERNEL_EVENTS_INTERNAL_H |
| 4 | |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 5 | #include <linux/hardirq.h> |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 7 | |
| 8 | /* Buffer handling */ |
| 9 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 10 | #define RING_BUFFER_WRITABLE 0x01 |
| 11 | |
| 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 */ |
Stephane Eranian | dd9c086 | 2013-03-18 14:33:28 +0100 | [diff] [blame] | 20 | int overwrite; /* can overwrite itself */ |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 21 | int paused; /* can write into ring buffer */ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 22 | |
| 23 | atomic_t poll; /* POLL_ for wakeups */ |
| 24 | |
| 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 */ |
| 30 | |
| 31 | long watermark; /* wakeup watermark */ |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 32 | long aux_watermark; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 33 | /* poll crap */ |
| 34 | spinlock_t event_lock; |
| 35 | struct list_head event_list; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 36 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 37 | atomic_t mmap_count; |
| 38 | unsigned long mmap_locked; |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 39 | struct user_struct *mmap_user; |
| 40 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 41 | /* AUX area */ |
Will Deacon | 2ab346c | 2017-08-16 17:18:16 +0100 | [diff] [blame] | 42 | long aux_head; |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 43 | local_t aux_nest; |
Will Deacon | d9a50b0 | 2017-08-16 17:18:17 +0100 | [diff] [blame] | 44 | long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 45 | unsigned long aux_pgoff; |
| 46 | int aux_nr_pages; |
Alexander Shishkin | 2023a0d | 2015-01-14 14:18:17 +0200 | [diff] [blame] | 47 | int aux_overwrite; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 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; |
| 54 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 55 | struct perf_event_mmap_page *user_page; |
| 56 | void *data_pages[0]; |
| 57 | }; |
| 58 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 59 | extern void rb_free(struct ring_buffer *rb); |
Peter Zijlstra | 57ffc5c | 2015-06-18 12:32:49 +0200 | [diff] [blame] | 60 | |
| 61 | static inline void rb_free_rcu(struct rcu_head *rcu_head) |
| 62 | { |
| 63 | struct ring_buffer *rb; |
| 64 | |
| 65 | rb = container_of(rcu_head, struct ring_buffer, rcu_head); |
| 66 | rb_free(rb); |
| 67 | } |
| 68 | |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 69 | static inline void rb_toggle_paused(struct ring_buffer *rb, bool pause) |
| 70 | { |
| 71 | if (!pause && rb->nr_pages) |
| 72 | rb->paused = 0; |
| 73 | else |
| 74 | rb->paused = 1; |
| 75 | } |
| 76 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 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); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 80 | extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event, |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 81 | pgoff_t pgoff, int nr_pages, long watermark, int flags); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 82 | extern void rb_free_aux(struct ring_buffer *rb); |
Alexander Shishkin | fdc2670 | 2015-01-14 14:18:16 +0200 | [diff] [blame] | 83 | extern struct ring_buffer *ring_buffer_get(struct perf_event *event); |
| 84 | extern void ring_buffer_put(struct ring_buffer *rb); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 85 | |
| 86 | static inline bool rb_has_aux(struct ring_buffer *rb) |
| 87 | { |
| 88 | return !!rb->aux_nr_pages; |
| 89 | } |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 90 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 91 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 92 | unsigned long size, u64 flags); |
| 93 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 94 | extern struct page * |
| 95 | perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff); |
| 96 | |
| 97 | #ifdef CONFIG_PERF_USE_VMALLOC |
| 98 | /* |
| 99 | * Back perf_mmap() with vmalloc memory. |
| 100 | * |
| 101 | * Required for architectures that have d-cache aliasing issues. |
| 102 | */ |
| 103 | |
| 104 | static inline int page_order(struct ring_buffer *rb) |
| 105 | { |
| 106 | return rb->page_order; |
| 107 | } |
| 108 | |
| 109 | #else |
| 110 | |
| 111 | static inline int page_order(struct ring_buffer *rb) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | #endif |
| 116 | |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 117 | static inline unsigned long perf_data_size(struct ring_buffer *rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 118 | { |
| 119 | return rb->nr_pages << (PAGE_SHIFT + page_order(rb)); |
| 120 | } |
| 121 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 122 | static inline unsigned long perf_aux_size(struct ring_buffer *rb) |
| 123 | { |
| 124 | return rb->aux_nr_pages << PAGE_SHIFT; |
| 125 | } |
| 126 | |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 127 | #define __DEFINE_OUTPUT_COPY_BODY(advance_buf, memcpy_func, ...) \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 128 | { \ |
| 129 | unsigned long size, written; \ |
| 130 | \ |
| 131 | do { \ |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 132 | size = min(handle->size, len); \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 133 | written = memcpy_func(__VA_ARGS__); \ |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 134 | written = size - written; \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 135 | \ |
| 136 | len -= written; \ |
| 137 | handle->addr += written; \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 138 | if (advance_buf) \ |
| 139 | buf += written; \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 140 | handle->size -= written; \ |
| 141 | if (!handle->size) { \ |
| 142 | struct ring_buffer *rb = handle->rb; \ |
| 143 | \ |
| 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); \ |
| 148 | } \ |
| 149 | } while (len && written == size); \ |
| 150 | \ |
| 151 | return len; \ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 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) \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 158 | __DEFINE_OUTPUT_COPY_BODY(true, memcpy_func, handle->addr, buf, size) |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 159 | |
| 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) |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 163 | { |
| 164 | unsigned long orig_len = len; |
| 165 | __DEFINE_OUTPUT_COPY_BODY(false, copy_func, handle->addr, buf, |
| 166 | orig_len - len, size) |
| 167 | } |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 168 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 169 | static inline unsigned long |
| 170 | memcpy_common(void *dst, const void *src, unsigned long n) |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 171 | { |
| 172 | memcpy(dst, src, n); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 173 | return 0; |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | DEFINE_OUTPUT_COPY(__output_copy, memcpy_common) |
| 177 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 178 | static inline unsigned long |
| 179 | memcpy_skip(void *dst, const void *src, unsigned long n) |
| 180 | { |
| 181 | return 0; |
| 182 | } |
Jiri Olsa | 5685e0f | 2012-08-07 15:20:39 +0200 | [diff] [blame] | 183 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 184 | DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip) |
Jiri Olsa | 5685e0f | 2012-08-07 15:20:39 +0200 | [diff] [blame] | 185 | |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 186 | #ifndef arch_perf_out_copy_user |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 187 | #define arch_perf_out_copy_user arch_perf_out_copy_user |
| 188 | |
| 189 | static inline unsigned long |
| 190 | arch_perf_out_copy_user(void *dst, const void *src, unsigned long n) |
| 191 | { |
| 192 | unsigned long ret; |
| 193 | |
| 194 | pagefault_disable(); |
| 195 | ret = __copy_from_user_inatomic(dst, src, n); |
| 196 | pagefault_enable(); |
| 197 | |
| 198 | return ret; |
| 199 | } |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 200 | #endif |
| 201 | |
| 202 | DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user) |
| 203 | |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 204 | /* Callchain handling */ |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 205 | extern struct perf_callchain_entry * |
| 206 | perf_callchain(struct perf_event *event, struct pt_regs *regs); |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 207 | |
| 208 | static inline int get_recursion_context(int *recursion) |
| 209 | { |
| 210 | int rctx; |
| 211 | |
Jesper Dangaard Brouer | d061841 | 2017-08-22 19:22:43 +0200 | [diff] [blame] | 212 | if (unlikely(in_nmi())) |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 213 | rctx = 3; |
| 214 | else if (in_irq()) |
| 215 | rctx = 2; |
| 216 | else if (in_softirq()) |
| 217 | rctx = 1; |
| 218 | else |
| 219 | rctx = 0; |
| 220 | |
| 221 | if (recursion[rctx]) |
| 222 | return -1; |
| 223 | |
| 224 | recursion[rctx]++; |
| 225 | barrier(); |
| 226 | |
| 227 | return rctx; |
| 228 | } |
| 229 | |
| 230 | static inline void put_recursion_context(int *recursion, int rctx) |
| 231 | { |
| 232 | barrier(); |
| 233 | recursion[rctx]--; |
| 234 | } |
| 235 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 236 | #ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP |
| 237 | static inline bool arch_perf_have_user_stack_dump(void) |
| 238 | { |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | #define perf_user_stack_pointer(regs) user_stack_pointer(regs) |
| 243 | #else |
| 244 | static inline bool arch_perf_have_user_stack_dump(void) |
| 245 | { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | #define perf_user_stack_pointer(regs) 0 |
| 250 | #endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */ |
| 251 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 252 | #endif /* _KERNEL_EVENTS_INTERNAL_H */ |