internal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef _KERNEL_EVENTS_INTERNAL_H
  2. #define _KERNEL_EVENTS_INTERNAL_H
  3. #include <linux/hardirq.h>
  4. #include <linux/uaccess.h>
  5. /* Buffer handling */
  6. #define RING_BUFFER_WRITABLE 0x01
  7. struct ring_buffer {
  8. atomic_t refcount;
  9. struct rcu_head rcu_head;
  10. #ifdef CONFIG_PERF_USE_VMALLOC
  11. struct work_struct work;
  12. int page_order; /* allocation order */
  13. #endif
  14. int nr_pages; /* nr of data pages */
  15. int overwrite; /* can overwrite itself */
  16. atomic_t poll; /* POLL_ for wakeups */
  17. local_t head; /* write position */
  18. local_t nest; /* nested writers */
  19. local_t events; /* event limit */
  20. local_t wakeup; /* wakeup stamp */
  21. local_t lost; /* nr records lost */
  22. long watermark; /* wakeup watermark */
  23. /* poll crap */
  24. spinlock_t event_lock;
  25. struct list_head event_list;
  26. atomic_t mmap_count;
  27. unsigned long mmap_locked;
  28. struct user_struct *mmap_user;
  29. struct perf_event_mmap_page *user_page;
  30. void *data_pages[0];
  31. };
  32. extern void rb_free(struct ring_buffer *rb);
  33. extern struct ring_buffer *
  34. rb_alloc(int nr_pages, long watermark, int cpu, int flags);
  35. extern void perf_event_wakeup(struct perf_event *event);
  36. extern void
  37. perf_event_header__init_id(struct perf_event_header *header,
  38. struct perf_sample_data *data,
  39. struct perf_event *event);
  40. extern void
  41. perf_event__output_id_sample(struct perf_event *event,
  42. struct perf_output_handle *handle,
  43. struct perf_sample_data *sample);
  44. extern struct page *
  45. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff);
  46. #ifdef CONFIG_PERF_USE_VMALLOC
  47. /*
  48. * Back perf_mmap() with vmalloc memory.
  49. *
  50. * Required for architectures that have d-cache aliasing issues.
  51. */
  52. static inline int page_order(struct ring_buffer *rb)
  53. {
  54. return rb->page_order;
  55. }
  56. #else
  57. static inline int page_order(struct ring_buffer *rb)
  58. {
  59. return 0;
  60. }
  61. #endif
  62. static inline unsigned long perf_data_size(struct ring_buffer *rb)
  63. {
  64. return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
  65. }
  66. #define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \
  67. static inline unsigned long \
  68. func_name(struct perf_output_handle *handle, \
  69. const void *buf, unsigned long len) \
  70. { \
  71. unsigned long size, written; \
  72. \
  73. do { \
  74. size = min(handle->size, len); \
  75. written = memcpy_func(handle->addr, buf, size); \
  76. written = size - written; \
  77. \
  78. len -= written; \
  79. handle->addr += written; \
  80. buf += written; \
  81. handle->size -= written; \
  82. if (!handle->size) { \
  83. struct ring_buffer *rb = handle->rb; \
  84. \
  85. handle->page++; \
  86. handle->page &= rb->nr_pages - 1; \
  87. handle->addr = rb->data_pages[handle->page]; \
  88. handle->size = PAGE_SIZE << page_order(rb); \
  89. } \
  90. } while (len && written == size); \
  91. \
  92. return len; \
  93. }
  94. static inline unsigned long
  95. memcpy_common(void *dst, const void *src, unsigned long n)
  96. {
  97. memcpy(dst, src, n);
  98. return 0;
  99. }
  100. DEFINE_OUTPUT_COPY(__output_copy, memcpy_common)
  101. static inline unsigned long
  102. memcpy_skip(void *dst, const void *src, unsigned long n)
  103. {
  104. return 0;
  105. }
  106. DEFINE_OUTPUT_COPY(__output_skip, memcpy_skip)
  107. #ifndef arch_perf_out_copy_user
  108. #define arch_perf_out_copy_user arch_perf_out_copy_user
  109. static inline unsigned long
  110. arch_perf_out_copy_user(void *dst, const void *src, unsigned long n)
  111. {
  112. unsigned long ret;
  113. pagefault_disable();
  114. ret = __copy_from_user_inatomic(dst, src, n);
  115. pagefault_enable();
  116. return ret;
  117. }
  118. #endif
  119. DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user)
  120. /* Callchain handling */
  121. extern struct perf_callchain_entry *
  122. perf_callchain(struct perf_event *event, struct pt_regs *regs);
  123. extern int get_callchain_buffers(void);
  124. extern void put_callchain_buffers(void);
  125. static inline int get_recursion_context(int *recursion)
  126. {
  127. int rctx;
  128. if (in_nmi())
  129. rctx = 3;
  130. else if (in_irq())
  131. rctx = 2;
  132. else if (in_softirq())
  133. rctx = 1;
  134. else
  135. rctx = 0;
  136. if (recursion[rctx])
  137. return -1;
  138. recursion[rctx]++;
  139. barrier();
  140. return rctx;
  141. }
  142. static inline void put_recursion_context(int *recursion, int rctx)
  143. {
  144. barrier();
  145. recursion[rctx]--;
  146. }
  147. #ifdef CONFIG_HAVE_PERF_USER_STACK_DUMP
  148. static inline bool arch_perf_have_user_stack_dump(void)
  149. {
  150. return true;
  151. }
  152. #define perf_user_stack_pointer(regs) user_stack_pointer(regs)
  153. #else
  154. static inline bool arch_perf_have_user_stack_dump(void)
  155. {
  156. return false;
  157. }
  158. #define perf_user_stack_pointer(regs) 0
  159. #endif /* CONFIG_HAVE_PERF_USER_STACK_DUMP */
  160. #endif /* _KERNEL_EVENTS_INTERNAL_H */