Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/oprofile.h> |
| 17 | #include <linux/sched.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/dcookies.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <asm/uaccess.h> |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "oprof.h" |
| 24 | #include "event_buffer.h" |
| 25 | #include "oprofile_stats.h" |
| 26 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 27 | DEFINE_MUTEX(buffer_mutex); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static unsigned long buffer_opened; |
| 30 | static DECLARE_WAIT_QUEUE_HEAD(buffer_wait); |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 31 | static unsigned long *event_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static unsigned long buffer_size; |
| 33 | static unsigned long buffer_watershed; |
| 34 | static size_t buffer_pos; |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 35 | /* atomic_t because wait_event checks it outside of buffer_mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static atomic_t buffer_ready = ATOMIC_INIT(0); |
| 37 | |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 38 | /* |
| 39 | * Add an entry to the event buffer. When we get near to the end we |
| 40 | * wake up the process sleeping on the read() of the file. To protect |
| 41 | * the event_buffer this function may only be called when buffer_mutex |
| 42 | * is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | void add_event_entry(unsigned long value) |
| 45 | { |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 46 | /* |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 47 | * This shouldn't happen since all workqueues or handlers are |
| 48 | * canceled or flushed before the event buffer is freed. |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 49 | */ |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 50 | if (!event_buffer) { |
| 51 | WARN_ON_ONCE(1); |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 52 | return; |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 53 | } |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (buffer_pos == buffer_size) { |
| 56 | atomic_inc(&oprofile_stats.event_lost_overflow); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | event_buffer[buffer_pos] = value; |
| 61 | if (++buffer_pos == buffer_size - buffer_watershed) { |
| 62 | atomic_set(&buffer_ready, 1); |
| 63 | wake_up(&buffer_wait); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* Wake up the waiting process if any. This happens |
| 69 | * on "echo 0 >/dev/oprofile/enable" so the daemon |
| 70 | * processes the data remaining in the event buffer. |
| 71 | */ |
| 72 | void wake_up_buffer_waiter(void) |
| 73 | { |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 74 | mutex_lock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | atomic_set(&buffer_ready, 1); |
| 76 | wake_up(&buffer_wait); |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 77 | mutex_unlock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | int alloc_event_buffer(void) |
| 82 | { |
Jiri Kosina | 4dfc896 | 2007-03-28 18:12:34 +0200 | [diff] [blame] | 83 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Jiri Kosina | 4dfc896 | 2007-03-28 18:12:34 +0200 | [diff] [blame] | 85 | spin_lock_irqsave(&oprofilefs_lock, flags); |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 86 | buffer_size = oprofile_buffer_size; |
| 87 | buffer_watershed = oprofile_buffer_watershed; |
Jiri Kosina | 4dfc896 | 2007-03-28 18:12:34 +0200 | [diff] [blame] | 88 | spin_unlock_irqrestore(&oprofilefs_lock, flags); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (buffer_watershed >= buffer_size) |
| 91 | return -EINVAL; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 92 | |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 93 | buffer_pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | event_buffer = vmalloc(sizeof(unsigned long) * buffer_size); |
| 95 | if (!event_buffer) |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 96 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 98 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | void free_event_buffer(void) |
| 103 | { |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 104 | mutex_lock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | vfree(event_buffer); |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 106 | buffer_pos = 0; |
Carl Love | f4156d1 | 2008-08-11 17:25:43 +1000 | [diff] [blame] | 107 | event_buffer = NULL; |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 108 | mutex_unlock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 111 | |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 112 | static int event_buffer_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | int err = -EPERM; |
| 115 | |
| 116 | if (!capable(CAP_SYS_ADMIN)) |
| 117 | return -EPERM; |
| 118 | |
Nick Piggin | cae042a | 2008-10-23 16:25:54 +0200 | [diff] [blame] | 119 | if (test_and_set_bit_lock(0, &buffer_opened)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -EBUSY; |
| 121 | |
| 122 | /* Register as a user of dcookies |
| 123 | * to ensure they persist for the lifetime of |
| 124 | * the open event file |
| 125 | */ |
| 126 | err = -EINVAL; |
| 127 | file->private_data = dcookie_register(); |
| 128 | if (!file->private_data) |
| 129 | goto out; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if ((err = oprofile_setup())) |
| 132 | goto fail; |
| 133 | |
| 134 | /* NB: the actual start happens from userspace |
| 135 | * echo 1 >/dev/oprofile/enable |
| 136 | */ |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | |
| 140 | fail: |
| 141 | dcookie_unregister(file->private_data); |
| 142 | out: |
Nick Piggin | cae042a | 2008-10-23 16:25:54 +0200 | [diff] [blame] | 143 | __clear_bit_unlock(0, &buffer_opened); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return err; |
| 145 | } |
| 146 | |
| 147 | |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 148 | static int event_buffer_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | oprofile_stop(); |
| 151 | oprofile_shutdown(); |
| 152 | dcookie_unregister(file->private_data); |
| 153 | buffer_pos = 0; |
| 154 | atomic_set(&buffer_ready, 0); |
Nick Piggin | cae042a | 2008-10-23 16:25:54 +0200 | [diff] [blame] | 155 | __clear_bit_unlock(0, &buffer_opened); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 160 | static ssize_t event_buffer_read(struct file *file, char __user *buf, |
| 161 | size_t count, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | int retval = -EINVAL; |
| 164 | size_t const max = buffer_size * sizeof(unsigned long); |
| 165 | |
| 166 | /* handling partial reads is more trouble than it's worth */ |
| 167 | if (count != max || *offset) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | wait_event_interruptible(buffer_wait, atomic_read(&buffer_ready)); |
| 171 | |
| 172 | if (signal_pending(current)) |
| 173 | return -EINTR; |
| 174 | |
| 175 | /* can't currently happen */ |
| 176 | if (!atomic_read(&buffer_ready)) |
| 177 | return -EAGAIN; |
| 178 | |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 179 | mutex_lock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Robert Richter | c086893 | 2009-10-09 03:17:44 +0200 | [diff] [blame] | 181 | /* May happen if the buffer is freed during pending reads. */ |
David Rientjes | 066b3aa | 2009-09-09 15:02:33 +0200 | [diff] [blame] | 182 | if (!event_buffer) { |
| 183 | retval = -EINTR; |
| 184 | goto out; |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | atomic_set(&buffer_ready, 0); |
| 188 | |
| 189 | retval = -EFAULT; |
| 190 | |
| 191 | count = buffer_pos * sizeof(unsigned long); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (copy_to_user(buf, event_buffer, count)) |
| 194 | goto out; |
| 195 | |
| 196 | retval = count; |
| 197 | buffer_pos = 0; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | out: |
Markus Armbruster | 59cc185 | 2006-06-25 05:47:33 -0700 | [diff] [blame] | 200 | mutex_unlock(&buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return retval; |
| 202 | } |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 203 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 204 | const struct file_operations event_buffer_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | .open = event_buffer_open, |
| 206 | .release = event_buffer_release, |
| 207 | .read = event_buffer_read, |
| 208 | }; |