Skip to content

Commit 91d7753

Browse files
fweisbecacmel
authored andcommitted
perf: Factor __output_copy to be usable with specific copy function
Adding a generic way to use __output_copy function with specific copy function via DEFINE_PERF_OUTPUT_COPY macro. Using this to add new __output_copy_user function, that provides output copy from user pointers. For x86 the copy_from_user_nmi function is used and __copy_from_user_inatomic for the rest of the architectures. This new function will be used in user stack dump on sample, coming in next patches. Signed-off-by: Jiri Olsa <jolsa@redhat.com> Cc: "Frank Ch. Eigler" <fche@redhat.com> Cc: Arun Sharma <asharma@fb.com> Cc: Benjamin Redelings <benjamin.redelings@nescent.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: Cyrill Gorcunov <gorcunov@openvz.org> Cc: Frank Ch. Eigler <fche@redhat.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Robert Richter <robert.richter@amd.com> Cc: Stephane Eranian <eranian@google.com> Cc: Tom Zanussi <tzanussi@gmail.com> Cc: Ulrich Drepper <drepper@gmail.com> Link: http://lkml.kernel.org/r/1344345647-11536-4-git-send-email-jolsa@redhat.com Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 4018994 commit 91d7753

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

‎arch/x86/include/asm/perf_event.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,6 @@ static inline void perf_check_microcode(void) { }
262262
static inline void amd_pmu_disable_virt(void) { }
263263
#endif
264264

265+
#define arch_perf_out_copy_user copy_from_user_nmi
266+
265267
#endif /* _ASM_X86_PERF_EVENT_H */

‎include/linux/perf_event.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ static inline bool has_branch_stack(struct perf_event *event)
13191319
extern int perf_output_begin(struct perf_output_handle *handle,
13201320
struct perf_event *event, unsigned int size);
13211321
extern void perf_output_end(struct perf_output_handle *handle);
1322-
extern void perf_output_copy(struct perf_output_handle *handle,
1322+
extern unsigned int perf_output_copy(struct perf_output_handle *handle,
13231323
const void *buf, unsigned int len);
13241324
extern int perf_swevent_get_recursion_context(void);
13251325
extern void perf_swevent_put_recursion_context(int rctx);

‎kernel/events/internal.h‎

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _KERNEL_EVENTS_INTERNAL_H
33

44
#include <linux/hardirq.h>
5+
#include <linux/uaccess.h>
56

67
/* Buffer handling */
78

@@ -76,30 +77,49 @@ static inline unsigned long perf_data_size(struct ring_buffer *rb)
7677
return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
7778
}
7879

79-
static inline void
80-
__output_copy(struct perf_output_handle *handle,
81-
const void *buf, unsigned int len)
80+
#define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \
81+
static inline unsigned int \
82+
func_name(struct perf_output_handle *handle, \
83+
const void *buf, unsigned int len) \
84+
{ \
85+
unsigned long size, written; \
86+
\
87+
do { \
88+
size = min_t(unsigned long, handle->size, len); \
89+
\
90+
written = memcpy_func(handle->addr, buf, size); \
91+
\
92+
len -= written; \
93+
handle->addr += written; \
94+
buf += written; \
95+
handle->size -= written; \
96+
if (!handle->size) { \
97+
struct ring_buffer *rb = handle->rb; \
98+
\
99+
handle->page++; \
100+
handle->page &= rb->nr_pages - 1; \
101+
handle->addr = rb->data_pages[handle->page]; \
102+
handle->size = PAGE_SIZE << page_order(rb); \
103+
} \
104+
} while (len && written == size); \
105+
\
106+
return len; \
107+
}
108+
109+
static inline int memcpy_common(void *dst, const void *src, size_t n)
82110
{
83-
do {
84-
unsigned long size = min_t(unsigned long, handle->size, len);
85-
86-
memcpy(handle->addr, buf, size);
87-
88-
len -= size;
89-
handle->addr += size;
90-
buf += size;
91-
handle->size -= size;
92-
if (!handle->size) {
93-
struct ring_buffer *rb = handle->rb;
94-
95-
handle->page++;
96-
handle->page &= rb->nr_pages - 1;
97-
handle->addr = rb->data_pages[handle->page];
98-
handle->size = PAGE_SIZE << page_order(rb);
99-
}
100-
} while (len);
111+
memcpy(dst, src, n);
112+
return n;
101113
}
102114

115+
DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
116+
117+
#ifndef arch_perf_out_copy_user
118+
#define arch_perf_out_copy_user __copy_from_user_inatomic
119+
#endif
120+
121+
DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
122+
103123
/* Callchain handling */
104124
extern struct perf_callchain_entry *
105125
perf_callchain(struct perf_event *event, struct pt_regs *regs);

‎kernel/events/ring_buffer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ int perf_output_begin(struct perf_output_handle *handle,
182182
return -ENOSPC;
183183
}
184184

185-
void perf_output_copy(struct perf_output_handle *handle,
185+
unsigned int perf_output_copy(struct perf_output_handle *handle,
186186
const void *buf, unsigned int len)
187187
{
188-
__output_copy(handle, buf, len);
188+
return __output_copy(handle, buf, len);
189189
}
190190

191191
void perf_output_end(struct perf_output_handle *handle)

0 commit comments

Comments
 (0)