event_buffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @file event_buffer.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. *
  9. * This is the global event buffer that the user-space
  10. * daemon reads from. The event buffer is an untyped array
  11. * of unsigned longs. Entries are prefixed by the
  12. * escape value ESCAPE_CODE followed by an identifying code.
  13. */
  14. #include <linux/vmalloc.h>
  15. #include <linux/oprofile.h>
  16. #include <linux/sched.h>
  17. #include <linux/capability.h>
  18. #include <linux/dcookies.h>
  19. #include <linux/fs.h>
  20. #include <asm/uaccess.h>
  21. #include "oprof.h"
  22. #include "event_buffer.h"
  23. #include "oprofile_stats.h"
  24. DEFINE_MUTEX(buffer_mutex);
  25. static unsigned long buffer_opened;
  26. static DECLARE_WAIT_QUEUE_HEAD(buffer_wait);
  27. static unsigned long *event_buffer;
  28. static unsigned long buffer_size;
  29. static unsigned long buffer_watershed;
  30. static size_t buffer_pos;
  31. /* atomic_t because wait_event checks it outside of buffer_mutex */
  32. static atomic_t buffer_ready = ATOMIC_INIT(0);
  33. /*
  34. * Add an entry to the event buffer. When we get near to the end we
  35. * wake up the process sleeping on the read() of the file. To protect
  36. * the event_buffer this function may only be called when buffer_mutex
  37. * is set.
  38. */
  39. void add_event_entry(unsigned long value)
  40. {
  41. /*
  42. * This shouldn't happen since all workqueues or handlers are
  43. * canceled or flushed before the event buffer is freed.
  44. */
  45. if (!event_buffer) {
  46. WARN_ON_ONCE(1);
  47. return;
  48. }
  49. if (buffer_pos == buffer_size) {
  50. atomic_inc(&oprofile_stats.event_lost_overflow);
  51. return;
  52. }
  53. event_buffer[buffer_pos] = value;
  54. if (++buffer_pos == buffer_size - buffer_watershed) {
  55. atomic_set(&buffer_ready, 1);
  56. wake_up(&buffer_wait);
  57. }
  58. }
  59. /* Wake up the waiting process if any. This happens
  60. * on "echo 0 >/dev/oprofile/enable" so the daemon
  61. * processes the data remaining in the event buffer.
  62. */
  63. void wake_up_buffer_waiter(void)
  64. {
  65. mutex_lock(&buffer_mutex);
  66. atomic_set(&buffer_ready, 1);
  67. wake_up(&buffer_wait);
  68. mutex_unlock(&buffer_mutex);
  69. }
  70. int alloc_event_buffer(void)
  71. {
  72. unsigned long flags;
  73. raw_spin_lock_irqsave(&oprofilefs_lock, flags);
  74. buffer_size = oprofile_buffer_size;
  75. buffer_watershed = oprofile_buffer_watershed;
  76. raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
  77. if (buffer_watershed >= buffer_size)
  78. return -EINVAL;
  79. buffer_pos = 0;
  80. event_buffer = vmalloc(sizeof(unsigned long) * buffer_size);
  81. if (!event_buffer)
  82. return -ENOMEM;
  83. return 0;
  84. }
  85. void free_event_buffer(void)
  86. {
  87. mutex_lock(&buffer_mutex);
  88. vfree(event_buffer);
  89. buffer_pos = 0;
  90. event_buffer = NULL;
  91. mutex_unlock(&buffer_mutex);
  92. }
  93. static int event_buffer_open(struct inode *inode, struct file *file)
  94. {
  95. int err = -EPERM;
  96. if (!capable(CAP_SYS_ADMIN))
  97. return -EPERM;
  98. if (test_and_set_bit_lock(0, &buffer_opened))
  99. return -EBUSY;
  100. /* Register as a user of dcookies
  101. * to ensure they persist for the lifetime of
  102. * the open event file
  103. */
  104. err = -EINVAL;
  105. file->private_data = dcookie_register();
  106. if (!file->private_data)
  107. goto out;
  108. if ((err = oprofile_setup()))
  109. goto fail;
  110. /* NB: the actual start happens from userspace
  111. * echo 1 >/dev/oprofile/enable
  112. */
  113. return nonseekable_open(inode, file);
  114. fail:
  115. dcookie_unregister(file->private_data);
  116. out:
  117. __clear_bit_unlock(0, &buffer_opened);
  118. return err;
  119. }
  120. static int event_buffer_release(struct inode *inode, struct file *file)
  121. {
  122. oprofile_stop();
  123. oprofile_shutdown();
  124. dcookie_unregister(file->private_data);
  125. buffer_pos = 0;
  126. atomic_set(&buffer_ready, 0);
  127. __clear_bit_unlock(0, &buffer_opened);
  128. return 0;
  129. }
  130. static ssize_t event_buffer_read(struct file *file, char __user *buf,
  131. size_t count, loff_t *offset)
  132. {
  133. int retval = -EINVAL;
  134. size_t const max = buffer_size * sizeof(unsigned long);
  135. /* handling partial reads is more trouble than it's worth */
  136. if (count != max || *offset)
  137. return -EINVAL;
  138. wait_event_interruptible(buffer_wait, atomic_read(&buffer_ready));
  139. if (signal_pending(current))
  140. return -EINTR;
  141. /* can't currently happen */
  142. if (!atomic_read(&buffer_ready))
  143. return -EAGAIN;
  144. mutex_lock(&buffer_mutex);
  145. /* May happen if the buffer is freed during pending reads. */
  146. if (!event_buffer) {
  147. retval = -EINTR;
  148. goto out;
  149. }
  150. atomic_set(&buffer_ready, 0);
  151. retval = -EFAULT;
  152. count = buffer_pos * sizeof(unsigned long);
  153. if (copy_to_user(buf, event_buffer, count))
  154. goto out;
  155. retval = count;
  156. buffer_pos = 0;
  157. out:
  158. mutex_unlock(&buffer_mutex);
  159. return retval;
  160. }
  161. const struct file_operations event_buffer_fops = {
  162. .open = event_buffer_open,
  163. .release = event_buffer_release,
  164. .read = event_buffer_read,
  165. .llseek = no_llseek,
  166. };