event_buffer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* Add an entry to the event buffer. When we
  34. * get near to the end we wake up the process
  35. * sleeping on the read() of the file.
  36. */
  37. void add_event_entry(unsigned long value)
  38. {
  39. /*
  40. * catch potential error
  41. */
  42. if (!event_buffer)
  43. return;
  44. if (buffer_pos == buffer_size) {
  45. atomic_inc(&oprofile_stats.event_lost_overflow);
  46. return;
  47. }
  48. event_buffer[buffer_pos] = value;
  49. if (++buffer_pos == buffer_size - buffer_watershed) {
  50. atomic_set(&buffer_ready, 1);
  51. wake_up(&buffer_wait);
  52. }
  53. }
  54. /* Wake up the waiting process if any. This happens
  55. * on "echo 0 >/dev/oprofile/enable" so the daemon
  56. * processes the data remaining in the event buffer.
  57. */
  58. void wake_up_buffer_waiter(void)
  59. {
  60. mutex_lock(&buffer_mutex);
  61. atomic_set(&buffer_ready, 1);
  62. wake_up(&buffer_wait);
  63. mutex_unlock(&buffer_mutex);
  64. }
  65. int alloc_event_buffer(void)
  66. {
  67. int err = -ENOMEM;
  68. unsigned long flags;
  69. spin_lock_irqsave(&oprofilefs_lock, flags);
  70. buffer_size = oprofile_buffer_size;
  71. buffer_watershed = oprofile_buffer_watershed;
  72. spin_unlock_irqrestore(&oprofilefs_lock, flags);
  73. if (buffer_watershed >= buffer_size)
  74. return -EINVAL;
  75. event_buffer = vmalloc(sizeof(unsigned long) * buffer_size);
  76. if (!event_buffer)
  77. goto out;
  78. err = 0;
  79. out:
  80. return err;
  81. }
  82. void free_event_buffer(void)
  83. {
  84. mutex_lock(&buffer_mutex);
  85. vfree(event_buffer);
  86. event_buffer = NULL;
  87. mutex_unlock(&buffer_mutex);
  88. }
  89. static int event_buffer_open(struct inode *inode, struct file *file)
  90. {
  91. int err = -EPERM;
  92. if (!capable(CAP_SYS_ADMIN))
  93. return -EPERM;
  94. if (test_and_set_bit_lock(0, &buffer_opened))
  95. return -EBUSY;
  96. /* Register as a user of dcookies
  97. * to ensure they persist for the lifetime of
  98. * the open event file
  99. */
  100. err = -EINVAL;
  101. file->private_data = dcookie_register();
  102. if (!file->private_data)
  103. goto out;
  104. if ((err = oprofile_setup()))
  105. goto fail;
  106. /* NB: the actual start happens from userspace
  107. * echo 1 >/dev/oprofile/enable
  108. */
  109. return 0;
  110. fail:
  111. dcookie_unregister(file->private_data);
  112. out:
  113. __clear_bit_unlock(0, &buffer_opened);
  114. return err;
  115. }
  116. static int event_buffer_release(struct inode *inode, struct file *file)
  117. {
  118. oprofile_stop();
  119. oprofile_shutdown();
  120. dcookie_unregister(file->private_data);
  121. buffer_pos = 0;
  122. atomic_set(&buffer_ready, 0);
  123. __clear_bit_unlock(0, &buffer_opened);
  124. return 0;
  125. }
  126. static ssize_t event_buffer_read(struct file *file, char __user *buf,
  127. size_t count, loff_t *offset)
  128. {
  129. int retval = -EINVAL;
  130. size_t const max = buffer_size * sizeof(unsigned long);
  131. /* handling partial reads is more trouble than it's worth */
  132. if (count != max || *offset)
  133. return -EINVAL;
  134. wait_event_interruptible(buffer_wait, atomic_read(&buffer_ready));
  135. if (signal_pending(current))
  136. return -EINTR;
  137. /* can't currently happen */
  138. if (!atomic_read(&buffer_ready))
  139. return -EAGAIN;
  140. mutex_lock(&buffer_mutex);
  141. if (!event_buffer) {
  142. retval = -EINTR;
  143. goto out;
  144. }
  145. atomic_set(&buffer_ready, 0);
  146. retval = -EFAULT;
  147. count = buffer_pos * sizeof(unsigned long);
  148. if (copy_to_user(buf, event_buffer, count))
  149. goto out;
  150. retval = count;
  151. buffer_pos = 0;
  152. out:
  153. mutex_unlock(&buffer_mutex);
  154. return retval;
  155. }
  156. const struct file_operations event_buffer_fops = {
  157. .open = event_buffer_open,
  158. .release = event_buffer_release,
  159. .read = event_buffer_read,
  160. };