[FIX] race condition on probes disarming (at stop)
[kernel/swap-modules.git] / writer / debugfs_writer.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  writer/debugfs_writer.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/debugfs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <asm/uaccess.h>
31 #include <driver/swap_debugfs.h>
32 #include "swap_writer_module.h"
33 #include "event_filter.h"
34
35
36 /* ============================================================================
37  * ===                               BUFFER                                 ===
38  * ============================================================================
39  */
40 static char *common_buf = NULL;
41 enum { subbuf_size = 8*1024 };
42 enum { common_buf_size = subbuf_size * NR_CPUS };
43
44 static int init_buffer(void)
45 {
46         common_buf = vmalloc(common_buf_size);
47
48         return common_buf ? 0 : -ENOMEM;
49 }
50
51 static void exit_buffer(void)
52 {
53         vfree(common_buf);
54         common_buf = NULL;
55 }
56
57 static void *get_current_buf(void)
58 {
59         return common_buf + subbuf_size * get_cpu();
60 }
61
62 static void put_current_buf(void)
63 {
64         put_cpu();
65 }
66
67
68
69
70
71 /* ============================================================================
72  * ===                             FOPS_RAW                                 ===
73  * ============================================================================
74  */
75 static ssize_t write_raw(struct file *file, const char __user *user_buf,
76                          size_t count, loff_t *ppos)
77 {
78         int ret;
79         void *buf;
80
81         if (count > subbuf_size)
82                 return -EINVAL;
83
84         buf = get_current_buf();
85         if (copy_from_user(buf, user_buf, count)) {
86                 ret = -EFAULT;
87                 goto put_buf;
88         }
89
90         ret = raw_msg(buf, count);
91
92 put_buf:
93         put_current_buf();
94         return ret;
95 }
96
97 static const struct file_operations fops_raw = {
98         .owner = THIS_MODULE,
99         .write =        write_raw,
100         .llseek =       default_llseek
101 };
102
103
104
105
106
107 /* ============================================================================
108  * ===                        FOPS_AVAILABLE_FILTERS                        ===
109  * ============================================================================
110  */
111 struct read_buf {
112         char *begin;
113         char *ptr;
114         char *end;
115 };
116
117 static void func_for_read(struct ev_filter *f, void *data)
118 {
119         struct read_buf *rbuf = (struct read_buf *)data;
120         int len = strlen(f->name);
121
122         if (rbuf->end - rbuf->ptr < len + 2)
123                 return;
124
125         if (rbuf->ptr != rbuf->begin) {
126                 *rbuf->ptr = ' ';
127                 ++rbuf->ptr;
128         }
129
130         memcpy(rbuf->ptr, f->name, len);
131         rbuf->ptr += len;
132 }
133
134 static ssize_t read_af(struct file *file, char __user *user_buf,
135                        size_t count, loff_t *ppos)
136 {
137         char buf[512];
138         struct read_buf rbuf = {
139                 .begin = buf,
140                 .ptr = buf,
141                 .end = buf + sizeof(buf)
142         };
143
144         event_filter_on_each(func_for_read, (void *)&rbuf);
145
146         *rbuf.ptr = '\n';
147         ++rbuf.ptr;
148
149         return simple_read_from_buffer(user_buf, count, ppos,
150                                        rbuf.begin, rbuf.ptr - rbuf.begin);
151 }
152
153 static const struct file_operations fops_available_filters = {
154         .owner = THIS_MODULE,
155         .read =         read_af,
156         .llseek =       default_llseek
157 };
158
159
160
161
162
163 /* ============================================================================
164  * ===                              FOPS_FILTER                             ===
165  * ============================================================================
166  */
167 static ssize_t read_filter(struct file *file, char __user *user_buf,
168                            size_t count, loff_t *ppos)
169 {
170         const char *name = event_filter_get();
171         int len = strlen(name);
172         char *buf;
173         ssize_t ret;
174
175         buf = kmalloc(len + 2, GFP_KERNEL);
176         memcpy(buf, name, len);
177
178         buf[len] = '\0';
179         buf[len + 1] = '\n';
180
181         ret = simple_read_from_buffer(user_buf, count, ppos, buf, len + 2);
182         kfree(buf);
183
184         return ret;
185 }
186
187 static ssize_t write_filter(struct file *file, const char __user *user_buf,
188                         size_t count, loff_t *ppos)
189 {
190         enum { len = 32 };
191         char buf[len], name[len];
192         size_t buf_size;
193         ssize_t ret;
194
195         buf_size = min(count, (size_t)(len - 1));
196         if (copy_from_user(buf, user_buf, buf_size))
197                 return -EFAULT;
198
199         buf[len - 1] = '\0';
200         ret = sscanf(buf, "%31s", name);
201         if (ret != 1)
202                 return -EINVAL;
203
204         ret = event_filter_set(name);
205         if (ret)
206                 return -EINVAL;
207
208         return count;
209 }
210
211 static const struct file_operations fops_filter = {
212         .owner = THIS_MODULE,
213         .read =         read_filter,
214         .write =        write_filter,
215         .llseek =       default_llseek
216 };
217
218
219
220
221
222 /* ============================================================================
223  * ===                              INIT/EXIT                               ===
224  * ============================================================================
225  */
226 static struct dentry *writer_dir = NULL;
227
228 void exit_debugfs_writer(void)
229 {
230         if (writer_dir)
231                 debugfs_remove_recursive(writer_dir);
232
233         writer_dir = NULL;
234
235         exit_buffer();
236 }
237
238 int init_debugfs_writer(void)
239 {
240         int ret;
241         struct dentry *swap_dir, *dentry;
242
243         ret = init_buffer();
244         if (ret)
245                 return ret;
246
247         swap_dir = get_swap_debugfs_dir();
248         if (swap_dir == NULL)
249                 return -ENOENT;
250
251         writer_dir = debugfs_create_dir("writer", swap_dir);
252         if (writer_dir == NULL)
253                 return -ENOMEM;
254
255         dentry = debugfs_create_file("raw", 0600, writer_dir, NULL, &fops_raw);
256         if (dentry == NULL)
257                 goto fail;
258
259         dentry = debugfs_create_file("available_filters", 0600, writer_dir, NULL, &fops_available_filters);
260         if (dentry == NULL)
261                 goto fail;
262
263         dentry = debugfs_create_file("filter", 0600, writer_dir, NULL, &fops_filter);
264         if (dentry == NULL)
265                 goto fail;
266
267         return 0;
268
269 fail:
270         exit_debugfs_writer();
271         return -ENOMEM;
272 }