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> |
Elena Reshetova | fecb8ed | 2019-01-28 14:27:27 +0200 | [diff] [blame] | 7 | #include <linux/refcount.h> |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 8 | |
| 9 | /* Buffer handling */ |
| 10 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 11 | #define RING_BUFFER_WRITABLE 0x01 |
| 12 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 13 | struct perf_buffer { |
Elena Reshetova | fecb8ed | 2019-01-28 14:27:27 +0200 | [diff] [blame] | 14 | refcount_t refcount; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 15 | struct rcu_head rcu_head; |
| 16 | #ifdef CONFIG_PERF_USE_VMALLOC |
| 17 | struct work_struct work; |
| 18 | int page_order; /* allocation order */ |
| 19 | #endif |
| 20 | int nr_pages; /* nr of data pages */ |
Stephane Eranian | dd9c086 | 2013-03-18 14:33:28 +0100 | [diff] [blame] | 21 | int overwrite; /* can overwrite itself */ |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 22 | int paused; /* can write into ring buffer */ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 23 | |
| 24 | atomic_t poll; /* POLL_ for wakeups */ |
| 25 | |
| 26 | local_t head; /* write position */ |
Peter Zijlstra | 5322ea5 | 2019-05-17 13:52:34 +0200 | [diff] [blame] | 27 | unsigned int nest; /* nested writers */ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 28 | local_t events; /* event limit */ |
| 29 | local_t wakeup; /* wakeup stamp */ |
| 30 | local_t lost; /* nr records lost */ |
| 31 | |
| 32 | long watermark; /* wakeup watermark */ |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 33 | long aux_watermark; |
Peter Zijlstra | 10c6db1 | 2011-11-26 02:47:31 +0100 | [diff] [blame] | 34 | /* poll crap */ |
| 35 | spinlock_t event_lock; |
| 36 | struct list_head event_list; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 37 | |
Peter Zijlstra | 9bb5d40 | 2013-06-04 10:44:21 +0200 | [diff] [blame] | 38 | atomic_t mmap_count; |
| 39 | unsigned long mmap_locked; |
Peter Zijlstra | 26cb63a | 2013-05-28 10:55:48 +0200 | [diff] [blame] | 40 | struct user_struct *mmap_user; |
| 41 | |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 42 | /* AUX area */ |
Will Deacon | 2ab346c | 2017-08-16 17:18:16 +0100 | [diff] [blame] | 43 | long aux_head; |
Peter Zijlstra | 5322ea5 | 2019-05-17 13:52:34 +0200 | [diff] [blame] | 44 | unsigned int aux_nest; |
Will Deacon | d9a50b0 | 2017-08-16 17:18:17 +0100 | [diff] [blame] | 45 | long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */ |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 46 | unsigned long aux_pgoff; |
| 47 | int aux_nr_pages; |
Alexander Shishkin | 2023a0d | 2015-01-14 14:18:17 +0200 | [diff] [blame] | 48 | int aux_overwrite; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 49 | atomic_t aux_mmap_count; |
| 50 | unsigned long aux_mmap_locked; |
| 51 | void (*free_aux)(void *); |
Elena Reshetova | ca3bb3d | 2019-01-28 14:27:28 +0200 | [diff] [blame] | 52 | refcount_t aux_refcount; |
Alexander Shishkin | a4faf00 | 2019-10-25 17:08:33 +0300 | [diff] [blame] | 53 | int aux_in_sampling; |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 54 | void **aux_pages; |
| 55 | void *aux_priv; |
| 56 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 57 | struct perf_event_mmap_page *user_page; |
Gustavo A. R. Silva | c50c75e | 2020-05-11 15:12:27 -0500 | [diff] [blame] | 58 | void *data_pages[]; |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 61 | extern void rb_free(struct perf_buffer *rb); |
Peter Zijlstra | 57ffc5c | 2015-06-18 12:32:49 +0200 | [diff] [blame] | 62 | |
| 63 | static inline void rb_free_rcu(struct rcu_head *rcu_head) |
| 64 | { |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 65 | struct perf_buffer *rb; |
Peter Zijlstra | 57ffc5c | 2015-06-18 12:32:49 +0200 | [diff] [blame] | 66 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 67 | rb = container_of(rcu_head, struct perf_buffer, rcu_head); |
Peter Zijlstra | 57ffc5c | 2015-06-18 12:32:49 +0200 | [diff] [blame] | 68 | rb_free(rb); |
| 69 | } |
| 70 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 71 | static inline void rb_toggle_paused(struct perf_buffer *rb, bool pause) |
Wang Nan | 86e7972 | 2016-03-28 06:41:29 +0000 | [diff] [blame] | 72 | { |
| 73 | if (!pause && rb->nr_pages) |
| 74 | rb->paused = 0; |
| 75 | else |
| 76 | rb->paused = 1; |
| 77 | } |
| 78 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 79 | extern struct perf_buffer * |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 80 | rb_alloc(int nr_pages, long watermark, int cpu, int flags); |
| 81 | extern void perf_event_wakeup(struct perf_event *event); |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 82 | extern int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event, |
Alexander Shishkin | 1a59413 | 2015-01-14 14:18:18 +0200 | [diff] [blame] | 83 | pgoff_t pgoff, int nr_pages, long watermark, int flags); |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 84 | extern void rb_free_aux(struct perf_buffer *rb); |
| 85 | extern struct perf_buffer *ring_buffer_get(struct perf_event *event); |
| 86 | extern void ring_buffer_put(struct perf_buffer *rb); |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 87 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 88 | static inline bool rb_has_aux(struct perf_buffer *rb) |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 89 | { |
| 90 | return !!rb->aux_nr_pages; |
| 91 | } |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 92 | |
Alexander Shishkin | 68db7e9 | 2015-01-14 14:18:15 +0200 | [diff] [blame] | 93 | void perf_event_aux_event(struct perf_event *event, unsigned long head, |
| 94 | unsigned long size, u64 flags); |
| 95 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 96 | extern struct page * |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 97 | perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff); |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 98 | |
| 99 | #ifdef CONFIG_PERF_USE_VMALLOC |
| 100 | /* |
| 101 | * Back perf_mmap() with vmalloc memory. |
| 102 | * |
| 103 | * Required for architectures that have d-cache aliasing issues. |
| 104 | */ |
| 105 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 106 | static inline int page_order(struct perf_buffer *rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 107 | { |
| 108 | return rb->page_order; |
| 109 | } |
| 110 | |
| 111 | #else |
| 112 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 113 | static inline int page_order(struct perf_buffer *rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 114 | { |
| 115 | return 0; |
| 116 | } |
| 117 | #endif |
| 118 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 119 | static inline unsigned long perf_data_size(struct perf_buffer *rb) |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 120 | { |
| 121 | return rb->nr_pages << (PAGE_SHIFT + page_order(rb)); |
| 122 | } |
| 123 | |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 124 | static inline unsigned long perf_aux_size(struct perf_buffer *rb) |
Peter Zijlstra | 45bfb2e | 2015-01-14 14:18:11 +0200 | [diff] [blame] | 125 | { |
| 126 | return rb->aux_nr_pages << PAGE_SHIFT; |
| 127 | } |
| 128 | |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 129 | #define __DEFINE_OUTPUT_COPY_BODY(advance_buf, memcpy_func, ...) \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 130 | { \ |
| 131 | unsigned long size, written; \ |
| 132 | \ |
| 133 | do { \ |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 134 | size = min(handle->size, len); \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 135 | written = memcpy_func(__VA_ARGS__); \ |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 136 | written = size - written; \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 137 | \ |
| 138 | len -= written; \ |
| 139 | handle->addr += written; \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 140 | if (advance_buf) \ |
| 141 | buf += written; \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 142 | handle->size -= written; \ |
| 143 | if (!handle->size) { \ |
Steven Rostedt (VMware) | 56de4e8 | 2019-12-13 13:21:30 -0500 | [diff] [blame] | 144 | struct perf_buffer *rb = handle->rb; \ |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 145 | \ |
| 146 | handle->page++; \ |
| 147 | handle->page &= rb->nr_pages - 1; \ |
| 148 | handle->addr = rb->data_pages[handle->page]; \ |
| 149 | handle->size = PAGE_SIZE << page_order(rb); \ |
| 150 | } \ |
| 151 | } while (len && written == size); \ |
| 152 | \ |
| 153 | return len; \ |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 156 | #define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \ |
| 157 | static inline unsigned long \ |
| 158 | func_name(struct perf_output_handle *handle, \ |
| 159 | const void *buf, unsigned long len) \ |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 160 | __DEFINE_OUTPUT_COPY_BODY(true, memcpy_func, handle->addr, buf, size) |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 161 | |
| 162 | static inline unsigned long |
| 163 | __output_custom(struct perf_output_handle *handle, perf_copy_f copy_func, |
| 164 | const void *buf, unsigned long len) |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 165 | { |
| 166 | unsigned long orig_len = len; |
| 167 | __DEFINE_OUTPUT_COPY_BODY(false, copy_func, handle->addr, buf, |
| 168 | orig_len - len, size) |
| 169 | } |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 170 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 171 | static inline unsigned long |
| 172 | memcpy_common(void *dst, const void *src, unsigned long n) |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 173 | { |
| 174 | memcpy(dst, src, n); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 175 | return 0; |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | DEFINE_OUTPUT_COPY(__output_copy, memcpy_common) |
| 179 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 180 | static inline unsigned long |
| 181 | memcpy_skip(void *dst, const void *src, unsigned long n) |
| 182 | { |
| 183 | return 0; |
| 184 | } |
Jiri Olsa | 5685e0f | 2012-08-07 15:20:39 +0200 | [diff] [blame] | 185 | |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 186 | DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip) |
Jiri Olsa | 5685e0f | 2012-08-07 15:20:39 +0200 | [diff] [blame] | 187 | |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 188 | #ifndef arch_perf_out_copy_user |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 189 | #define arch_perf_out_copy_user arch_perf_out_copy_user |
| 190 | |
| 191 | static inline unsigned long |
| 192 | arch_perf_out_copy_user(void *dst, const void *src, unsigned long n) |
| 193 | { |
| 194 | unsigned long ret; |
| 195 | |
| 196 | pagefault_disable(); |
| 197 | ret = __copy_from_user_inatomic(dst, src, n); |
| 198 | pagefault_enable(); |
| 199 | |
| 200 | return ret; |
| 201 | } |
Frederic Weisbecker | 91d7753 | 2012-08-07 15:20:38 +0200 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user) |
| 205 | |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 206 | static inline int get_recursion_context(int *recursion) |
| 207 | { |
Peter Zijlstra | 09da9c8 | 2020-10-30 13:43:16 +0100 | [diff] [blame] | 208 | unsigned int pc = preempt_count(); |
| 209 | unsigned char rctx = 0; |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 210 | |
Peter Zijlstra | 09da9c8 | 2020-10-30 13:43:16 +0100 | [diff] [blame] | 211 | rctx += !!(pc & (NMI_MASK)); |
| 212 | rctx += !!(pc & (NMI_MASK | HARDIRQ_MASK)); |
| 213 | rctx += !!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)); |
Borislav Petkov | 9251f90 | 2011-10-16 17:15:04 +0200 | [diff] [blame] | 214 | |
| 215 | if (recursion[rctx]) |
| 216 | return -1; |
| 217 | |
| 218 | recursion[rctx]++; |
| 219 | barrier(); |
| 220 | |
| 221 | return rctx; |
| 222 | } |
| 223 | |
| 224 | static inline void put_recursion_context(int *recursion, int rctx) |
| 225 | { |
| 226 | barrier(); |
| 227 | recursion[rctx]--; |
| 228 | } |
| 229 | |
Jiri Olsa | c5ebced | 2012-08-07 15:20:40 +0200 | [diff] [blame] | 230 | #ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP |
| 231 | static inline bool arch_perf_have_user_stack_dump(void) |
| 232 | { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | #define perf_user_stack_pointer(regs) user_stack_pointer(regs) |
| 237 | #else |
| 238 | static inline bool arch_perf_have_user_stack_dump(void) |
| 239 | { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | #define perf_user_stack_pointer(regs) 0 |
| 244 | #endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */ |
| 245 | |
Frederic Weisbecker | 7636913 | 2011-05-19 19:55:04 +0200 | [diff] [blame] | 246 | #endif /* _KERNEL_EVENTS_INTERNAL_H */ |