internal.h 5.3 KB
Newer Older
1 2 3
#ifndef _KERNEL_EVENTS_INTERNAL_H
#define _KERNEL_EVENTS_INTERNAL_H

4
#include <linux/hardirq.h>
5
#include <linux/uaccess.h>
6 7 8

/* Buffer handling */

9 10 11 12 13 14 15 16 17 18
#define RING_BUFFER_WRITABLE		0x01

struct ring_buffer {
	atomic_t			refcount;
	struct rcu_head			rcu_head;
#ifdef CONFIG_PERF_USE_VMALLOC
	struct work_struct		work;
	int				page_order;	/* allocation order  */
#endif
	int				nr_pages;	/* nr of data pages  */
19
	int				overwrite;	/* can overwrite itself */
20 21 22 23 24 25 26 27 28 29

	atomic_t			poll;		/* POLL_ for wakeups */

	local_t				head;		/* write position    */
	local_t				nest;		/* nested writers    */
	local_t				events;		/* event limit       */
	local_t				wakeup;		/* wakeup stamp      */
	local_t				lost;		/* nr records lost   */

	long				watermark;	/* wakeup watermark  */
30 31 32
	/* poll crap */
	spinlock_t			event_lock;
	struct list_head		event_list;
33

34 35
	atomic_t			mmap_count;
	unsigned long			mmap_locked;
P
Peter Zijlstra 已提交
36 37
	struct user_struct		*mmap_user;

38
	/* AUX area */
39 40
	local_t				aux_head;
	local_t				aux_nest;
41 42
	unsigned long			aux_pgoff;
	int				aux_nr_pages;
43
	int				aux_overwrite;
44 45 46 47 48 49 50
	atomic_t			aux_mmap_count;
	unsigned long			aux_mmap_locked;
	void				(*free_aux)(void *);
	atomic_t			aux_refcount;
	void				**aux_pages;
	void				*aux_priv;

51 52 53 54 55 56 57 58
	struct perf_event_mmap_page	*user_page;
	void				*data_pages[0];
};

extern void rb_free(struct ring_buffer *rb);
extern struct ring_buffer *
rb_alloc(int nr_pages, long watermark, int cpu, int flags);
extern void perf_event_wakeup(struct perf_event *event);
59 60 61
extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
			pgoff_t pgoff, int nr_pages, int flags);
extern void rb_free_aux(struct ring_buffer *rb);
62 63
extern struct ring_buffer *ring_buffer_get(struct perf_event *event);
extern void ring_buffer_put(struct ring_buffer *rb);
64 65 66 67 68

static inline bool rb_has_aux(struct ring_buffer *rb)
{
	return !!rb->aux_nr_pages;
}
69

A
Alexander Shishkin 已提交
70 71 72
void perf_event_aux_event(struct perf_event *event, unsigned long head,
			  unsigned long size, u64 flags);

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
extern void
perf_event_header__init_id(struct perf_event_header *header,
			   struct perf_sample_data *data,
			   struct perf_event *event);
extern void
perf_event__output_id_sample(struct perf_event *event,
			     struct perf_output_handle *handle,
			     struct perf_sample_data *sample);

extern struct page *
perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);

#ifdef CONFIG_PERF_USE_VMALLOC
/*
 * Back perf_mmap() with vmalloc memory.
 *
 * Required for architectures that have d-cache aliasing issues.
 */

static inline int page_order(struct ring_buffer *rb)
{
	return rb->page_order;
}

#else

static inline int page_order(struct ring_buffer *rb)
{
	return 0;
}
#endif

105
static inline unsigned long perf_data_size(struct ring_buffer *rb)
106 107 108 109
{
	return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
}

110 111 112 113 114
static inline unsigned long perf_aux_size(struct ring_buffer *rb)
{
	return rb->aux_nr_pages << PAGE_SHIFT;
}

115
#define DEFINE_OUTPUT_COPY(func_name, memcpy_func)			\
116
static inline unsigned long						\
117
func_name(struct perf_output_handle *handle,				\
118
	  const void *buf, unsigned long len)				\
119 120 121 122
{									\
	unsigned long size, written;					\
									\
	do {								\
123
		size    = min(handle->size, len);			\
124
		written = memcpy_func(handle->addr, buf, size);		\
125
		written = size - written;				\
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
									\
		len -= written;						\
		handle->addr += written;				\
		buf += written;						\
		handle->size -= written;				\
		if (!handle->size) {					\
			struct ring_buffer *rb = handle->rb;		\
									\
			handle->page++;					\
			handle->page &= rb->nr_pages - 1;		\
			handle->addr = rb->data_pages[handle->page];	\
			handle->size = PAGE_SIZE << page_order(rb);	\
		}							\
	} while (len && written == size);				\
									\
	return len;							\
}

144 145
static inline unsigned long
memcpy_common(void *dst, const void *src, unsigned long n)
146
{
147
	memcpy(dst, src, n);
148
	return 0;
149 150
}

151 152
DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)

153 154 155 156 157
static inline unsigned long
memcpy_skip(void *dst, const void *src, unsigned long n)
{
	return 0;
}
158

159
DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
160

161
#ifndef arch_perf_out_copy_user
162 163 164 165 166 167 168 169 170 171 172 173 174
#define arch_perf_out_copy_user arch_perf_out_copy_user

static inline unsigned long
arch_perf_out_copy_user(void *dst, const void *src, unsigned long n)
{
	unsigned long ret;

	pagefault_disable();
	ret = __copy_from_user_inatomic(dst, src, n);
	pagefault_enable();

	return ret;
}
175 176 177 178
#endif

DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)

179
/* Callchain handling */
180 181
extern struct perf_callchain_entry *
perf_callchain(struct perf_event *event, struct pt_regs *regs);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
extern int get_callchain_buffers(void);
extern void put_callchain_buffers(void);

static inline int get_recursion_context(int *recursion)
{
	int rctx;

	if (in_nmi())
		rctx = 3;
	else if (in_irq())
		rctx = 2;
	else if (in_softirq())
		rctx = 1;
	else
		rctx = 0;

	if (recursion[rctx])
		return -1;

	recursion[rctx]++;
	barrier();

	return rctx;
}

static inline void put_recursion_context(int *recursion, int rctx)
{
	barrier();
	recursion[rctx]--;
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
static inline bool arch_perf_have_user_stack_dump(void)
{
	return true;
}

#define perf_user_stack_pointer(regs) user_stack_pointer(regs)
#else
static inline bool arch_perf_have_user_stack_dump(void)
{
	return false;
}

#define perf_user_stack_pointer(regs) 0
#endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */

229
#endif /* _KERNEL_EVENTS_INTERNAL_H */