Linux Audio

Check our new training course

Loading...
v3.15
 
  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
 11struct 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	struct perf_event_mmap_page	*user_page;
 39	void				*data_pages[0];
 40};
 41
 42extern void rb_free(struct ring_buffer *rb);
 43extern struct ring_buffer *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44rb_alloc(int nr_pages, long watermark, int cpu, int flags);
 45extern void perf_event_wakeup(struct perf_event *event);
 
 
 
 
 
 
 
 
 
 
 46
 47extern void
 48perf_event_header__init_id(struct perf_event_header *header,
 49			   struct perf_sample_data *data,
 50			   struct perf_event *event);
 51extern void
 52perf_event__output_id_sample(struct perf_event *event,
 53			     struct perf_output_handle *handle,
 54			     struct perf_sample_data *sample);
 55
 56extern struct page *
 57perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);
 58
 59#ifdef CONFIG_PERF_USE_VMALLOC
 60/*
 61 * Back perf_mmap() with vmalloc memory.
 62 *
 63 * Required for architectures that have d-cache aliasing issues.
 64 */
 65
 66static inline int page_order(struct ring_buffer *rb)
 67{
 68	return rb->page_order;
 69}
 70
 71#else
 72
 73static inline int page_order(struct ring_buffer *rb)
 74{
 75	return 0;
 76}
 77#endif
 78
 79static inline unsigned long perf_data_size(struct ring_buffer *rb)
 80{
 81	return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
 82}
 83
 84#define DEFINE_OUTPUT_COPY(func_name, memcpy_func)			\
 85static inline unsigned long						\
 86func_name(struct perf_output_handle *handle,				\
 87	  const void *buf, unsigned long len)				\
 
 
 88{									\
 89	unsigned long size, written;					\
 90									\
 91	do {								\
 92		size    = min(handle->size, len);			\
 93		written = memcpy_func(handle->addr, buf, size);		\
 94		written = size - written;				\
 95									\
 96		len -= written;						\
 97		handle->addr += written;				\
 98		buf += written;						\
 
 99		handle->size -= written;				\
100		if (!handle->size) {					\
101			struct ring_buffer *rb = handle->rb;		\
102									\
103			handle->page++;					\
104			handle->page &= rb->nr_pages - 1;		\
105			handle->addr = rb->data_pages[handle->page];	\
106			handle->size = PAGE_SIZE << page_order(rb);	\
107		}							\
108	} while (len && written == size);				\
109									\
110	return len;							\
111}
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113static inline unsigned long
114memcpy_common(void *dst, const void *src, unsigned long n)
115{
116	memcpy(dst, src, n);
117	return 0;
118}
119
120DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
121
122static inline unsigned long
123memcpy_skip(void *dst, const void *src, unsigned long n)
124{
125	return 0;
126}
127
128DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
129
130#ifndef arch_perf_out_copy_user
131#define arch_perf_out_copy_user arch_perf_out_copy_user
132
133static inline unsigned long
134arch_perf_out_copy_user(void *dst, const void *src, unsigned long n)
135{
136	unsigned long ret;
137
138	pagefault_disable();
139	ret = __copy_from_user_inatomic(dst, src, n);
140	pagefault_enable();
141
142	return ret;
143}
144#endif
145
146DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
147
148/* Callchain handling */
149extern struct perf_callchain_entry *
150perf_callchain(struct perf_event *event, struct pt_regs *regs);
151extern int get_callchain_buffers(void);
152extern void put_callchain_buffers(void);
153
154static inline int get_recursion_context(int *recursion)
155{
156	int rctx;
157
158	if (in_nmi())
159		rctx = 3;
160	else if (in_irq())
161		rctx = 2;
162	else if (in_softirq())
163		rctx = 1;
164	else
165		rctx = 0;
166
167	if (recursion[rctx])
168		return -1;
169
170	recursion[rctx]++;
171	barrier();
172
173	return rctx;
174}
175
176static inline void put_recursion_context(int *recursion, int rctx)
177{
178	barrier();
179	recursion[rctx]--;
180}
181
182#ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
183static inline bool arch_perf_have_user_stack_dump(void)
184{
185	return true;
186}
187
188#define perf_user_stack_pointer(regs) user_stack_pointer(regs)
189#else
190static inline bool arch_perf_have_user_stack_dump(void)
191{
192	return false;
193}
194
195#define perf_user_stack_pointer(regs) 0
196#endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */
197
198#endif /* _KERNEL_EVENTS_INTERNAL_H */
v5.9
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef _KERNEL_EVENTS_INTERNAL_H
  3#define _KERNEL_EVENTS_INTERNAL_H
  4
  5#include <linux/hardirq.h>
  6#include <linux/uaccess.h>
  7#include <linux/refcount.h>
  8
  9/* Buffer handling */
 10
 11#define RING_BUFFER_WRITABLE		0x01
 12
 13struct perf_buffer {
 14	refcount_t			refcount;
 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  */
 21	int				overwrite;	/* can overwrite itself */
 22	int				paused;		/* can write into ring buffer */
 23
 24	atomic_t			poll;		/* POLL_ for wakeups */
 25
 26	local_t				head;		/* write position    */
 27	unsigned int			nest;		/* nested writers    */
 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  */
 33	long				aux_watermark;
 34	/* poll crap */
 35	spinlock_t			event_lock;
 36	struct list_head		event_list;
 37
 38	atomic_t			mmap_count;
 39	unsigned long			mmap_locked;
 40	struct user_struct		*mmap_user;
 41
 42	/* AUX area */
 43	long				aux_head;
 44	unsigned int			aux_nest;
 45	long				aux_wakeup;	/* last aux_watermark boundary crossed by aux_head */
 46	unsigned long			aux_pgoff;
 47	int				aux_nr_pages;
 48	int				aux_overwrite;
 49	atomic_t			aux_mmap_count;
 50	unsigned long			aux_mmap_locked;
 51	void				(*free_aux)(void *);
 52	refcount_t			aux_refcount;
 53	int				aux_in_sampling;
 54	void				**aux_pages;
 55	void				*aux_priv;
 56
 57	struct perf_event_mmap_page	*user_page;
 58	void				*data_pages[];
 59};
 60
 61extern void rb_free(struct perf_buffer *rb);
 62
 63static inline void rb_free_rcu(struct rcu_head *rcu_head)
 64{
 65	struct perf_buffer *rb;
 66
 67	rb = container_of(rcu_head, struct perf_buffer, rcu_head);
 68	rb_free(rb);
 69}
 70
 71static inline void rb_toggle_paused(struct perf_buffer *rb, bool pause)
 72{
 73	if (!pause && rb->nr_pages)
 74		rb->paused = 0;
 75	else
 76		rb->paused = 1;
 77}
 78
 79extern struct perf_buffer *
 80rb_alloc(int nr_pages, long watermark, int cpu, int flags);
 81extern void perf_event_wakeup(struct perf_event *event);
 82extern int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
 83			pgoff_t pgoff, int nr_pages, long watermark, int flags);
 84extern void rb_free_aux(struct perf_buffer *rb);
 85extern struct perf_buffer *ring_buffer_get(struct perf_event *event);
 86extern void ring_buffer_put(struct perf_buffer *rb);
 87
 88static inline bool rb_has_aux(struct perf_buffer *rb)
 89{
 90	return !!rb->aux_nr_pages;
 91}
 92
 93void perf_event_aux_event(struct perf_event *event, unsigned long head,
 94			  unsigned long size, u64 flags);
 
 
 
 
 
 
 95
 96extern struct page *
 97perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff);
 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
106static inline int page_order(struct perf_buffer *rb)
107{
108	return rb->page_order;
109}
110
111#else
112
113static inline int page_order(struct perf_buffer *rb)
114{
115	return 0;
116}
117#endif
118
119static inline unsigned long perf_data_size(struct perf_buffer *rb)
120{
121	return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
122}
123
124static inline unsigned long perf_aux_size(struct perf_buffer *rb)
125{
126	return rb->aux_nr_pages << PAGE_SHIFT;
127}
128
129#define __DEFINE_OUTPUT_COPY_BODY(advance_buf, memcpy_func, ...)	\
130{									\
131	unsigned long size, written;					\
132									\
133	do {								\
134		size    = min(handle->size, len);			\
135		written = memcpy_func(__VA_ARGS__);			\
136		written = size - written;				\
137									\
138		len -= written;						\
139		handle->addr += written;				\
140		if (advance_buf)					\
141			buf += written;					\
142		handle->size -= written;				\
143		if (!handle->size) {					\
144			struct perf_buffer *rb = handle->rb;	\
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;							\
154}
155
156#define DEFINE_OUTPUT_COPY(func_name, memcpy_func)			\
157static inline unsigned long						\
158func_name(struct perf_output_handle *handle,				\
159	  const void *buf, unsigned long len)				\
160__DEFINE_OUTPUT_COPY_BODY(true, memcpy_func, handle->addr, buf, size)
161
162static inline unsigned long
163__output_custom(struct perf_output_handle *handle, perf_copy_f copy_func,
164		const void *buf, unsigned long len)
165{
166	unsigned long orig_len = len;
167	__DEFINE_OUTPUT_COPY_BODY(false, copy_func, handle->addr, buf,
168				  orig_len - len, size)
169}
170
171static inline unsigned long
172memcpy_common(void *dst, const void *src, unsigned long n)
173{
174	memcpy(dst, src, n);
175	return 0;
176}
177
178DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
179
180static inline unsigned long
181memcpy_skip(void *dst, const void *src, unsigned long n)
182{
183	return 0;
184}
185
186DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
187
188#ifndef arch_perf_out_copy_user
189#define arch_perf_out_copy_user arch_perf_out_copy_user
190
191static inline unsigned long
192arch_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}
202#endif
203
204DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
205
 
 
 
 
 
 
206static inline int get_recursion_context(int *recursion)
207{
208	int rctx;
209
210	if (unlikely(in_nmi()))
211		rctx = 3;
212	else if (in_irq())
213		rctx = 2;
214	else if (in_softirq())
215		rctx = 1;
216	else
217		rctx = 0;
218
219	if (recursion[rctx])
220		return -1;
221
222	recursion[rctx]++;
223	barrier();
224
225	return rctx;
226}
227
228static inline void put_recursion_context(int *recursion, int rctx)
229{
230	barrier();
231	recursion[rctx]--;
232}
233
234#ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
235static inline bool arch_perf_have_user_stack_dump(void)
236{
237	return true;
238}
239
240#define perf_user_stack_pointer(regs) user_stack_pointer(regs)
241#else
242static inline bool arch_perf_have_user_stack_dump(void)
243{
244	return false;
245}
246
247#define perf_user_stack_pointer(regs) 0
248#endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */
249
250#endif /* _KERNEL_EVENTS_INTERNAL_H */