ksym_tracer: Fix memory leak
[safe/jmp/linux-2.6] / kernel / trace / trace_ksym.c
1 /*
2  * trace_ksym.c - Kernel Symbol Tracer
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) IBM Corporation, 2009
19  */
20
21 #include <linux/kallsyms.h>
22 #include <linux/uaccess.h>
23 #include <linux/debugfs.h>
24 #include <linux/ftrace.h>
25 #include <linux/module.h>
26 #include <linux/fs.h>
27
28 #include "trace_output.h"
29 #include "trace_stat.h"
30 #include "trace.h"
31
32 /* For now, let us restrict the no. of symbols traced simultaneously to number
33  * of available hardware breakpoint registers.
34  */
35 #define KSYM_TRACER_MAX HBP_NUM
36
37 #define KSYM_TRACER_OP_LEN 3 /* rw- */
38
39 struct trace_ksym {
40         struct hw_breakpoint    *ksym_hbp;
41         unsigned long           ksym_addr;
42 #ifdef CONFIG_PROFILE_KSYM_TRACER
43         unsigned long           counter;
44 #endif
45         struct hlist_node       ksym_hlist;
46 };
47
48 static struct trace_array *ksym_trace_array;
49
50 static unsigned int ksym_filter_entry_count;
51 static unsigned int ksym_tracing_enabled;
52
53 static HLIST_HEAD(ksym_filter_head);
54
55 static DEFINE_MUTEX(ksym_tracer_mutex);
56
57 #ifdef CONFIG_PROFILE_KSYM_TRACER
58
59 #define MAX_UL_INT 0xffffffff
60
61 void ksym_collect_stats(unsigned long hbp_hit_addr)
62 {
63         struct hlist_node *node;
64         struct trace_ksym *entry;
65
66         rcu_read_lock();
67         hlist_for_each_entry_rcu(entry, node, &ksym_filter_head, ksym_hlist) {
68                 if ((entry->ksym_addr == hbp_hit_addr) &&
69                     (entry->counter <= MAX_UL_INT)) {
70                         entry->counter++;
71                         break;
72                 }
73         }
74         rcu_read_unlock();
75 }
76 #endif /* CONFIG_PROFILE_KSYM_TRACER */
77
78 void ksym_hbp_handler(struct hw_breakpoint *hbp, struct pt_regs *regs)
79 {
80         struct ring_buffer_event *event;
81         struct trace_array *tr;
82         struct ksym_trace_entry *entry;
83         int pc;
84
85         if (!ksym_tracing_enabled)
86                 return;
87
88         tr = ksym_trace_array;
89         pc = preempt_count();
90
91         event = trace_buffer_lock_reserve(tr, TRACE_KSYM,
92                                                         sizeof(*entry), 0, pc);
93         if (!event)
94                 return;
95
96         entry           = ring_buffer_event_data(event);
97         entry->ip       = instruction_pointer(regs);
98         entry->type     = hbp->info.type;
99         strlcpy(entry->ksym_name, hbp->info.name, KSYM_SYMBOL_LEN);
100         strlcpy(entry->cmd, current->comm, TASK_COMM_LEN);
101
102 #ifdef CONFIG_PROFILE_KSYM_TRACER
103         ksym_collect_stats(hbp->info.address);
104 #endif /* CONFIG_PROFILE_KSYM_TRACER */
105
106         trace_buffer_unlock_commit(tr, event, 0, pc);
107 }
108
109 /* Valid access types are represented as
110  *
111  * rw- : Set Read/Write Access Breakpoint
112  * -w- : Set Write Access Breakpoint
113  * --- : Clear Breakpoints
114  * --x : Set Execution Break points (Not available yet)
115  *
116  */
117 static int ksym_trace_get_access_type(char *str)
118 {
119         int access = 0;
120
121         if (str[0] == 'r')
122                 access += 4;
123         else if (str[0] != '-')
124                 return -EINVAL;
125
126         if (str[1] == 'w')
127                 access += 2;
128         else if (str[1] != '-')
129                 return -EINVAL;
130
131         if (str[2] != '-')
132                 return -EINVAL;
133
134         switch (access) {
135         case 6:
136                 access = HW_BREAKPOINT_RW;
137                 break;
138         case 2:
139                 access = HW_BREAKPOINT_WRITE;
140                 break;
141         }
142
143         return access;
144 }
145
146 /*
147  * There can be several possible malformed requests and we attempt to capture
148  * all of them. We enumerate some of the rules
149  * 1. We will not allow kernel symbols with ':' since it is used as a delimiter.
150  *    i.e. multiple ':' symbols disallowed. Possible uses are of the form
151  *    <module>:<ksym_name>:<op>.
152  * 2. No delimiter symbol ':' in the input string
153  * 3. Spurious operator symbols or symbols not in their respective positions
154  * 4. <ksym_name>:--- i.e. clear breakpoint request when ksym_name not in file
155  * 5. Kernel symbol not a part of /proc/kallsyms
156  * 6. Duplicate requests
157  */
158 static int parse_ksym_trace_str(char *input_string, char **ksymname,
159                                                         unsigned long *addr)
160 {
161         int ret;
162
163         strstrip(input_string);
164
165         *ksymname = strsep(&input_string, ":");
166         *addr = kallsyms_lookup_name(*ksymname);
167
168         /* Check for malformed request: (2), (1) and (5) */
169         if ((!input_string) ||
170             (strlen(input_string) != KSYM_TRACER_OP_LEN) ||
171             (*addr == 0))
172                 return -EINVAL;;
173
174         ret = ksym_trace_get_access_type(input_string);
175
176         return ret;
177 }
178
179 int process_new_ksym_entry(char *ksymname, int op, unsigned long addr)
180 {
181         struct trace_ksym *entry;
182         int ret = -ENOMEM;
183
184         if (ksym_filter_entry_count >= KSYM_TRACER_MAX) {
185                 printk(KERN_ERR "ksym_tracer: Maximum limit:(%d) reached. No"
186                 " new requests for tracing can be accepted now.\n",
187                         KSYM_TRACER_MAX);
188                 return -ENOSPC;
189         }
190
191         entry = kzalloc(sizeof(struct trace_ksym), GFP_KERNEL);
192         if (!entry)
193                 return -ENOMEM;
194
195         entry->ksym_hbp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
196         if (!entry->ksym_hbp)
197                 goto err;
198
199         entry->ksym_hbp->info.name = kstrdup(ksymname, GFP_KERNEL);
200         if (!entry->ksym_hbp->info.name)
201                 goto err;
202
203         entry->ksym_hbp->info.type = op;
204         entry->ksym_addr = entry->ksym_hbp->info.address = addr;
205 #ifdef CONFIG_X86
206         entry->ksym_hbp->info.len = HW_BREAKPOINT_LEN_4;
207 #endif
208         entry->ksym_hbp->triggered = (void *)ksym_hbp_handler;
209
210         ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
211         if (ret < 0) {
212                 printk(KERN_INFO "ksym_tracer request failed. Try again"
213                                         " later!!\n");
214                 ret = -EAGAIN;
215                 goto err;
216         }
217         hlist_add_head_rcu(&(entry->ksym_hlist), &ksym_filter_head);
218         ksym_filter_entry_count++;
219         return 0;
220 err:
221         if (entry->ksym_hbp)
222                 kfree(entry->ksym_hbp->info.name);
223         kfree(entry->ksym_hbp);
224         kfree(entry);
225         return ret;
226 }
227
228 static ssize_t ksym_trace_filter_read(struct file *filp, char __user *ubuf,
229                                                 size_t count, loff_t *ppos)
230 {
231         struct trace_ksym *entry;
232         struct hlist_node *node;
233         struct trace_seq *s;
234         ssize_t cnt = 0;
235         int ret;
236
237         s = kmalloc(sizeof(*s), GFP_KERNEL);
238         if (!s)
239                 return -ENOMEM;
240         trace_seq_init(s);
241
242         mutex_lock(&ksym_tracer_mutex);
243
244         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
245                 ret = trace_seq_printf(s, "%s:", entry->ksym_hbp->info.name);
246                 if (entry->ksym_hbp->info.type == HW_BREAKPOINT_WRITE)
247                         ret = trace_seq_puts(s, "-w-\n");
248                 else if (entry->ksym_hbp->info.type == HW_BREAKPOINT_RW)
249                         ret = trace_seq_puts(s, "rw-\n");
250                 WARN_ON_ONCE(!ret);
251         }
252
253         cnt = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
254
255         mutex_unlock(&ksym_tracer_mutex);
256
257         kfree(s);
258
259         return cnt;
260 }
261
262 static ssize_t ksym_trace_filter_write(struct file *file,
263                                         const char __user *buffer,
264                                                 size_t count, loff_t *ppos)
265 {
266         struct trace_ksym *entry;
267         struct hlist_node *node;
268         char *input_string, *ksymname = NULL;
269         unsigned long ksym_addr = 0;
270         int ret, op, changed = 0;
271
272         input_string = kzalloc(count + 1, GFP_KERNEL);
273         if (!input_string)
274                 return -ENOMEM;
275
276         if (copy_from_user(input_string, buffer, count)) {
277                 kfree(input_string);
278                 return -EFAULT;
279         }
280         input_string[count] = '\0';
281
282         ret = op = parse_ksym_trace_str(input_string, &ksymname, &ksym_addr);
283         if (ret < 0) {
284                 kfree(input_string);
285                 return ret;
286         }
287
288         mutex_lock(&ksym_tracer_mutex);
289
290         ret = -EINVAL;
291         hlist_for_each_entry(entry, node, &ksym_filter_head, ksym_hlist) {
292                 if (entry->ksym_addr == ksym_addr) {
293                         /* Check for malformed request: (6) */
294                         if (entry->ksym_hbp->info.type != op)
295                                 changed = 1;
296                         else
297                                 goto out;
298                         break;
299                 }
300         }
301         if (changed) {
302                 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
303                 entry->ksym_hbp->info.type = op;
304                 if (op > 0) {
305                         ret = register_kernel_hw_breakpoint(entry->ksym_hbp);
306                         if (ret == 0)
307                                 goto out;
308                 }
309                 ksym_filter_entry_count--;
310                 hlist_del_rcu(&(entry->ksym_hlist));
311                 synchronize_rcu();
312                 kfree(entry->ksym_hbp->info.name);
313                 kfree(entry->ksym_hbp);
314                 kfree(entry);
315                 goto out;
316         } else {
317                 /* Check for malformed request: (4) */
318                 if (op == 0)
319                         goto out;
320                 ret = process_new_ksym_entry(ksymname, op, ksym_addr);
321         }
322 out:
323         mutex_unlock(&ksym_tracer_mutex);
324
325         kfree(input_string);
326
327         if (!ret)
328                 ret = count;
329         return ret;
330 }
331
332 static const struct file_operations ksym_tracing_fops = {
333         .open           = tracing_open_generic,
334         .read           = ksym_trace_filter_read,
335         .write          = ksym_trace_filter_write,
336 };
337
338 static void ksym_trace_reset(struct trace_array *tr)
339 {
340         struct trace_ksym *entry;
341         struct hlist_node *node, *node1;
342
343         ksym_tracing_enabled = 0;
344
345         mutex_lock(&ksym_tracer_mutex);
346         hlist_for_each_entry_safe(entry, node, node1, &ksym_filter_head,
347                                                                 ksym_hlist) {
348                 unregister_kernel_hw_breakpoint(entry->ksym_hbp);
349                 ksym_filter_entry_count--;
350                 hlist_del_rcu(&(entry->ksym_hlist));
351                 synchronize_rcu();
352                 kfree(entry->ksym_hbp->info.name);
353                 kfree(entry->ksym_hbp);
354                 kfree(entry);
355         }
356         mutex_unlock(&ksym_tracer_mutex);
357 }
358
359 static int ksym_trace_init(struct trace_array *tr)
360 {
361         int cpu, ret = 0;
362
363         for_each_online_cpu(cpu)
364                 tracing_reset(tr, cpu);
365         ksym_tracing_enabled = 1;
366         ksym_trace_array = tr;
367
368         return ret;
369 }
370
371 static void ksym_trace_print_header(struct seq_file *m)
372 {
373
374         seq_puts(m,
375                  "#       TASK-PID      CPU#      Symbol         Type    "
376                  "Function         \n");
377         seq_puts(m,
378                  "#          |           |          |              |         "
379                  "|            \n");
380 }
381
382 static enum print_line_t ksym_trace_output(struct trace_iterator *iter)
383 {
384         struct trace_entry *entry = iter->ent;
385         struct trace_seq *s = &iter->seq;
386         struct ksym_trace_entry *field;
387         char str[KSYM_SYMBOL_LEN];
388         int ret;
389
390         if (entry->type != TRACE_KSYM)
391                 return TRACE_TYPE_UNHANDLED;
392
393         trace_assign_type(field, entry);
394
395         ret = trace_seq_printf(s, "%-15s %-5d %-3d %-20s ", field->cmd,
396                                 entry->pid, iter->cpu, field->ksym_name);
397         if (!ret)
398                 return TRACE_TYPE_PARTIAL_LINE;
399
400         switch (field->type) {
401         case HW_BREAKPOINT_WRITE:
402                 ret = trace_seq_printf(s, " W  ");
403                 break;
404         case HW_BREAKPOINT_RW:
405                 ret = trace_seq_printf(s, " RW ");
406                 break;
407         default:
408                 return TRACE_TYPE_PARTIAL_LINE;
409         }
410
411         if (!ret)
412                 return TRACE_TYPE_PARTIAL_LINE;
413
414         sprint_symbol(str, field->ip);
415         ret = trace_seq_printf(s, "%-20s\n", str);
416         if (!ret)
417                 return TRACE_TYPE_PARTIAL_LINE;
418
419         return TRACE_TYPE_HANDLED;
420 }
421
422 struct tracer ksym_tracer __read_mostly =
423 {
424         .name           = "ksym_tracer",
425         .init           = ksym_trace_init,
426         .reset          = ksym_trace_reset,
427 #ifdef CONFIG_FTRACE_SELFTEST
428         .selftest       = trace_selftest_startup_ksym,
429 #endif
430         .print_header   = ksym_trace_print_header,
431         .print_line     = ksym_trace_output
432 };
433
434 __init static int init_ksym_trace(void)
435 {
436         struct dentry *d_tracer;
437         struct dentry *entry;
438
439         d_tracer = tracing_init_dentry();
440         ksym_filter_entry_count = 0;
441
442         entry = debugfs_create_file("ksym_trace_filter", 0644, d_tracer,
443                                     NULL, &ksym_tracing_fops);
444         if (!entry)
445                 pr_warning("Could not create debugfs "
446                            "'ksym_trace_filter' file\n");
447
448         return register_tracer(&ksym_tracer);
449 }
450 device_initcall(init_ksym_trace);
451
452
453 #ifdef CONFIG_PROFILE_KSYM_TRACER
454 static int ksym_tracer_stat_headers(struct seq_file *m)
455 {
456         seq_printf(m, "   Access type    ");
457         seq_printf(m, "            Symbol                     Counter     \n");
458         return 0;
459 }
460
461 static int ksym_tracer_stat_show(struct seq_file *m, void *v)
462 {
463         struct hlist_node *stat = v;
464         struct trace_ksym *entry;
465         int access_type = 0;
466         char fn_name[KSYM_NAME_LEN];
467
468         entry = hlist_entry(stat, struct trace_ksym, ksym_hlist);
469
470         if (entry->ksym_hbp)
471                 access_type = entry->ksym_hbp->info.type;
472
473         switch (access_type) {
474         case HW_BREAKPOINT_WRITE:
475                 seq_printf(m, "     W     ");
476                 break;
477         case HW_BREAKPOINT_RW:
478                 seq_printf(m, "     RW    ");
479                 break;
480         default:
481                 seq_printf(m, "     NA    ");
482         }
483
484         if (lookup_symbol_name(entry->ksym_addr, fn_name) >= 0)
485                 seq_printf(m, "               %s                 ", fn_name);
486         else
487                 seq_printf(m, "               <NA>                ");
488
489         seq_printf(m, "%15lu\n", entry->counter);
490         return 0;
491 }
492
493 static void *ksym_tracer_stat_start(struct tracer_stat *trace)
494 {
495         return &(ksym_filter_head.first);
496 }
497
498 static void *
499 ksym_tracer_stat_next(void *v, int idx)
500 {
501         struct hlist_node *stat = v;
502
503         return stat->next;
504 }
505
506 static struct tracer_stat ksym_tracer_stats = {
507         .name = "ksym_tracer",
508         .stat_start = ksym_tracer_stat_start,
509         .stat_next = ksym_tracer_stat_next,
510         .stat_headers = ksym_tracer_stat_headers,
511         .stat_show = ksym_tracer_stat_show
512 };
513
514 __init static int ksym_tracer_stat_init(void)
515 {
516         int ret;
517
518         ret = register_stat_tracer(&ksym_tracer_stats);
519         if (ret) {
520                 printk(KERN_WARNING "Warning: could not register "
521                                     "ksym tracer stats\n");
522                 return 1;
523         }
524
525         return 0;
526 }
527 fs_initcall(ksym_tracer_stat_init);
528 #endif /* CONFIG_PROFILE_KSYM_TRACER */