Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[pandora-kernel.git] / lib / dynamic_debug.c
1 /*
2  * lib/dynamic_debug.c
3  *
4  * make pr_debug()/dev_dbg() calls runtime configurable based upon their
5  * source module.
6  *
7  * Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
8  * By Greg Banks <gnb@melbourne.sgi.com>
9  * Copyright (c) 2008 Silicon Graphics Inc.  All Rights Reserved.
10  * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/kallsyms.h>
17 #include <linux/types.h>
18 #include <linux/mutex.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/list.h>
22 #include <linux/sysctl.h>
23 #include <linux/ctype.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
26 #include <linux/dynamic_debug.h>
27 #include <linux/debugfs.h>
28 #include <linux/slab.h>
29 #include <linux/jump_label.h>
30 #include <linux/hardirq.h>
31 #include <linux/sched.h>
32
33 extern struct _ddebug __start___verbose[];
34 extern struct _ddebug __stop___verbose[];
35
36 struct ddebug_table {
37         struct list_head link;
38         char *mod_name;
39         unsigned int num_ddebugs;
40         unsigned int num_enabled;
41         struct _ddebug *ddebugs;
42 };
43
44 struct ddebug_query {
45         const char *filename;
46         const char *module;
47         const char *function;
48         const char *format;
49         unsigned int first_lineno, last_lineno;
50 };
51
52 struct ddebug_iter {
53         struct ddebug_table *table;
54         unsigned int idx;
55 };
56
57 static DEFINE_MUTEX(ddebug_lock);
58 static LIST_HEAD(ddebug_tables);
59 static int verbose = 0;
60
61 /* Return the last part of a pathname */
62 static inline const char *basename(const char *path)
63 {
64         const char *tail = strrchr(path, '/');
65         return tail ? tail+1 : path;
66 }
67
68 static struct { unsigned flag:8; char opt_char; } opt_array[] = {
69         { _DPRINTK_FLAGS_PRINT, 'p' },
70         { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
71         { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
72         { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
73         { _DPRINTK_FLAGS_INCL_TID, 't' },
74 };
75
76 /* format a string into buf[] which describes the _ddebug's flags */
77 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
78                                     size_t maxlen)
79 {
80         char *p = buf;
81         int i;
82
83         BUG_ON(maxlen < 4);
84         for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
85                 if (dp->flags & opt_array[i].flag)
86                         *p++ = opt_array[i].opt_char;
87         if (p == buf)
88                 *p++ = '-';
89         *p = '\0';
90
91         return buf;
92 }
93
94 /*
95  * Search the tables for _ddebug's which match the given
96  * `query' and apply the `flags' and `mask' to them.  Tells
97  * the user which ddebug's were changed, or whether none
98  * were matched.
99  */
100 static void ddebug_change(const struct ddebug_query *query,
101                            unsigned int flags, unsigned int mask)
102 {
103         int i;
104         struct ddebug_table *dt;
105         unsigned int newflags;
106         unsigned int nfound = 0;
107         char flagbuf[8];
108
109         /* search for matching ddebugs */
110         mutex_lock(&ddebug_lock);
111         list_for_each_entry(dt, &ddebug_tables, link) {
112
113                 /* match against the module name */
114                 if (query->module != NULL &&
115                     strcmp(query->module, dt->mod_name))
116                         continue;
117
118                 for (i = 0 ; i < dt->num_ddebugs ; i++) {
119                         struct _ddebug *dp = &dt->ddebugs[i];
120
121                         /* match against the source filename */
122                         if (query->filename != NULL &&
123                             strcmp(query->filename, dp->filename) &&
124                             strcmp(query->filename, basename(dp->filename)))
125                                 continue;
126
127                         /* match against the function */
128                         if (query->function != NULL &&
129                             strcmp(query->function, dp->function))
130                                 continue;
131
132                         /* match against the format */
133                         if (query->format != NULL &&
134                             strstr(dp->format, query->format) == NULL)
135                                 continue;
136
137                         /* match against the line number range */
138                         if (query->first_lineno &&
139                             dp->lineno < query->first_lineno)
140                                 continue;
141                         if (query->last_lineno &&
142                             dp->lineno > query->last_lineno)
143                                 continue;
144
145                         nfound++;
146
147                         newflags = (dp->flags & mask) | flags;
148                         if (newflags == dp->flags)
149                                 continue;
150
151                         if (!newflags)
152                                 dt->num_enabled--;
153                         else if (!dp->flags)
154                                 dt->num_enabled++;
155                         dp->flags = newflags;
156                         if (newflags)
157                                 dp->enabled = 1;
158                         else
159                                 dp->enabled = 0;
160                         if (verbose)
161                                 printk(KERN_INFO
162                                         "ddebug: changed %s:%d [%s]%s %s\n",
163                                         dp->filename, dp->lineno,
164                                         dt->mod_name, dp->function,
165                                         ddebug_describe_flags(dp, flagbuf,
166                                                         sizeof(flagbuf)));
167                 }
168         }
169         mutex_unlock(&ddebug_lock);
170
171         if (!nfound && verbose)
172                 printk(KERN_INFO "ddebug: no matches for query\n");
173 }
174
175 /*
176  * Split the buffer `buf' into space-separated words.
177  * Handles simple " and ' quoting, i.e. without nested,
178  * embedded or escaped \".  Return the number of words
179  * or <0 on error.
180  */
181 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
182 {
183         int nwords = 0;
184
185         while (*buf) {
186                 char *end;
187
188                 /* Skip leading whitespace */
189                 buf = skip_spaces(buf);
190                 if (!*buf)
191                         break;  /* oh, it was trailing whitespace */
192
193                 /* Run `end' over a word, either whitespace separated or quoted */
194                 if (*buf == '"' || *buf == '\'') {
195                         int quote = *buf++;
196                         for (end = buf ; *end && *end != quote ; end++)
197                                 ;
198                         if (!*end)
199                                 return -EINVAL; /* unclosed quote */
200                 } else {
201                         for (end = buf ; *end && !isspace(*end) ; end++)
202                                 ;
203                         BUG_ON(end == buf);
204                 }
205                 /* Here `buf' is the start of the word, `end' is one past the end */
206
207                 if (nwords == maxwords)
208                         return -EINVAL; /* ran out of words[] before bytes */
209                 if (*end)
210                         *end++ = '\0';  /* terminate the word */
211                 words[nwords++] = buf;
212                 buf = end;
213         }
214
215         if (verbose) {
216                 int i;
217                 printk(KERN_INFO "%s: split into words:", __func__);
218                 for (i = 0 ; i < nwords ; i++)
219                         printk(" \"%s\"", words[i]);
220                 printk("\n");
221         }
222
223         return nwords;
224 }
225
226 /*
227  * Parse a single line number.  Note that the empty string ""
228  * is treated as a special case and converted to zero, which
229  * is later treated as a "don't care" value.
230  */
231 static inline int parse_lineno(const char *str, unsigned int *val)
232 {
233         char *end = NULL;
234         BUG_ON(str == NULL);
235         if (*str == '\0') {
236                 *val = 0;
237                 return 0;
238         }
239         *val = simple_strtoul(str, &end, 10);
240         return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
241 }
242
243 /*
244  * Undo octal escaping in a string, inplace.  This is useful to
245  * allow the user to express a query which matches a format
246  * containing embedded spaces.
247  */
248 #define isodigit(c)             ((c) >= '0' && (c) <= '7')
249 static char *unescape(char *str)
250 {
251         char *in = str;
252         char *out = str;
253
254         while (*in) {
255                 if (*in == '\\') {
256                         if (in[1] == '\\') {
257                                 *out++ = '\\';
258                                 in += 2;
259                                 continue;
260                         } else if (in[1] == 't') {
261                                 *out++ = '\t';
262                                 in += 2;
263                                 continue;
264                         } else if (in[1] == 'n') {
265                                 *out++ = '\n';
266                                 in += 2;
267                                 continue;
268                         } else if (isodigit(in[1]) &&
269                                  isodigit(in[2]) &&
270                                  isodigit(in[3])) {
271                                 *out++ = ((in[1] - '0')<<6) |
272                                           ((in[2] - '0')<<3) |
273                                           (in[3] - '0');
274                                 in += 4;
275                                 continue;
276                         }
277                 }
278                 *out++ = *in++;
279         }
280         *out = '\0';
281
282         return str;
283 }
284
285 /*
286  * Parse words[] as a ddebug query specification, which is a series
287  * of (keyword, value) pairs chosen from these possibilities:
288  *
289  * func <function-name>
290  * file <full-pathname>
291  * file <base-filename>
292  * module <module-name>
293  * format <escaped-string-to-find-in-format>
294  * line <lineno>
295  * line <first-lineno>-<last-lineno> // where either may be empty
296  */
297 static int ddebug_parse_query(char *words[], int nwords,
298                                struct ddebug_query *query)
299 {
300         unsigned int i;
301
302         /* check we have an even number of words */
303         if (nwords % 2 != 0)
304                 return -EINVAL;
305         memset(query, 0, sizeof(*query));
306
307         for (i = 0 ; i < nwords ; i += 2) {
308                 if (!strcmp(words[i], "func"))
309                         query->function = words[i+1];
310                 else if (!strcmp(words[i], "file"))
311                         query->filename = words[i+1];
312                 else if (!strcmp(words[i], "module"))
313                         query->module = words[i+1];
314                 else if (!strcmp(words[i], "format"))
315                         query->format = unescape(words[i+1]);
316                 else if (!strcmp(words[i], "line")) {
317                         char *first = words[i+1];
318                         char *last = strchr(first, '-');
319                         if (last)
320                                 *last++ = '\0';
321                         if (parse_lineno(first, &query->first_lineno) < 0)
322                                 return -EINVAL;
323                         if (last != NULL) {
324                                 /* range <first>-<last> */
325                                 if (parse_lineno(last, &query->last_lineno) < 0)
326                                         return -EINVAL;
327                         } else {
328                                 query->last_lineno = query->first_lineno;
329                         }
330                 } else {
331                         if (verbose)
332                                 printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
333                                         __func__, words[i]);
334                         return -EINVAL;
335                 }
336         }
337
338         if (verbose)
339                 printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
340                        "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
341                         __func__, query->function, query->filename,
342                         query->module, query->format, query->first_lineno,
343                         query->last_lineno);
344
345         return 0;
346 }
347
348 /*
349  * Parse `str' as a flags specification, format [-+=][p]+.
350  * Sets up *maskp and *flagsp to be used when changing the
351  * flags fields of matched _ddebug's.  Returns 0 on success
352  * or <0 on error.
353  */
354 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
355                                unsigned int *maskp)
356 {
357         unsigned flags = 0;
358         int op = '=', i;
359
360         switch (*str) {
361         case '+':
362         case '-':
363         case '=':
364                 op = *str++;
365                 break;
366         default:
367                 return -EINVAL;
368         }
369         if (verbose)
370                 printk(KERN_INFO "%s: op='%c'\n", __func__, op);
371
372         for ( ; *str ; ++str) {
373                 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
374                         if (*str == opt_array[i].opt_char) {
375                                 flags |= opt_array[i].flag;
376                                 break;
377                         }
378                 }
379                 if (i < 0)
380                         return -EINVAL;
381         }
382         if (flags == 0)
383                 return -EINVAL;
384         if (verbose)
385                 printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
386
387         /* calculate final *flagsp, *maskp according to mask and op */
388         switch (op) {
389         case '=':
390                 *maskp = 0;
391                 *flagsp = flags;
392                 break;
393         case '+':
394                 *maskp = ~0U;
395                 *flagsp = flags;
396                 break;
397         case '-':
398                 *maskp = ~flags;
399                 *flagsp = 0;
400                 break;
401         }
402         if (verbose)
403                 printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
404                         __func__, *flagsp, *maskp);
405         return 0;
406 }
407
408 static int ddebug_exec_query(char *query_string)
409 {
410         unsigned int flags = 0, mask = 0;
411         struct ddebug_query query;
412 #define MAXWORDS 9
413         int nwords;
414         char *words[MAXWORDS];
415
416         nwords = ddebug_tokenize(query_string, words, MAXWORDS);
417         if (nwords <= 0)
418                 return -EINVAL;
419         if (ddebug_parse_query(words, nwords-1, &query))
420                 return -EINVAL;
421         if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
422                 return -EINVAL;
423
424         /* actually go and implement the change */
425         ddebug_change(&query, flags, mask);
426         return 0;
427 }
428
429 int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
430 {
431         va_list args;
432         int res;
433
434         BUG_ON(!descriptor);
435         BUG_ON(!fmt);
436
437         va_start(args, fmt);
438         res = printk(KERN_DEBUG);
439         if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
440                 if (in_interrupt())
441                         res += printk(KERN_CONT "<intr> ");
442                 else
443                         res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
444         }
445         if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
446                 res += printk(KERN_CONT "%s:", descriptor->modname);
447         if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
448                 res += printk(KERN_CONT "%s:", descriptor->function);
449         if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
450                 res += printk(KERN_CONT "%d ", descriptor->lineno);
451         res += vprintk(fmt, args);
452         va_end(args);
453
454         return res;
455 }
456 EXPORT_SYMBOL(__dynamic_pr_debug);
457
458 static __initdata char ddebug_setup_string[1024];
459 static __init int ddebug_setup_query(char *str)
460 {
461         if (strlen(str) >= 1024) {
462                 pr_warning("ddebug boot param string too large\n");
463                 return 0;
464         }
465         strcpy(ddebug_setup_string, str);
466         return 1;
467 }
468
469 __setup("ddebug_query=", ddebug_setup_query);
470
471 /*
472  * File_ops->write method for <debugfs>/dynamic_debug/conrol.  Gathers the
473  * command text from userspace, parses and executes it.
474  */
475 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
476                                   size_t len, loff_t *offp)
477 {
478         char tmpbuf[256];
479         int ret;
480
481         if (len == 0)
482                 return 0;
483         /* we don't check *offp -- multiple writes() are allowed */
484         if (len > sizeof(tmpbuf)-1)
485                 return -E2BIG;
486         if (copy_from_user(tmpbuf, ubuf, len))
487                 return -EFAULT;
488         tmpbuf[len] = '\0';
489         if (verbose)
490                 printk(KERN_INFO "%s: read %d bytes from userspace\n",
491                         __func__, (int)len);
492
493         ret = ddebug_exec_query(tmpbuf);
494         if (ret)
495                 return ret;
496
497         *offp += len;
498         return len;
499 }
500
501 /*
502  * Set the iterator to point to the first _ddebug object
503  * and return a pointer to that first object.  Returns
504  * NULL if there are no _ddebugs at all.
505  */
506 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
507 {
508         if (list_empty(&ddebug_tables)) {
509                 iter->table = NULL;
510                 iter->idx = 0;
511                 return NULL;
512         }
513         iter->table = list_entry(ddebug_tables.next,
514                                  struct ddebug_table, link);
515         iter->idx = 0;
516         return &iter->table->ddebugs[iter->idx];
517 }
518
519 /*
520  * Advance the iterator to point to the next _ddebug
521  * object from the one the iterator currently points at,
522  * and returns a pointer to the new _ddebug.  Returns
523  * NULL if the iterator has seen all the _ddebugs.
524  */
525 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
526 {
527         if (iter->table == NULL)
528                 return NULL;
529         if (++iter->idx == iter->table->num_ddebugs) {
530                 /* iterate to next table */
531                 iter->idx = 0;
532                 if (list_is_last(&iter->table->link, &ddebug_tables)) {
533                         iter->table = NULL;
534                         return NULL;
535                 }
536                 iter->table = list_entry(iter->table->link.next,
537                                          struct ddebug_table, link);
538         }
539         return &iter->table->ddebugs[iter->idx];
540 }
541
542 /*
543  * Seq_ops start method.  Called at the start of every
544  * read() call from userspace.  Takes the ddebug_lock and
545  * seeks the seq_file's iterator to the given position.
546  */
547 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
548 {
549         struct ddebug_iter *iter = m->private;
550         struct _ddebug *dp;
551         int n = *pos;
552
553         if (verbose)
554                 printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
555                         __func__, m, (unsigned long long)*pos);
556
557         mutex_lock(&ddebug_lock);
558
559         if (!n)
560                 return SEQ_START_TOKEN;
561         if (n < 0)
562                 return NULL;
563         dp = ddebug_iter_first(iter);
564         while (dp != NULL && --n > 0)
565                 dp = ddebug_iter_next(iter);
566         return dp;
567 }
568
569 /*
570  * Seq_ops next method.  Called several times within a read()
571  * call from userspace, with ddebug_lock held.  Walks to the
572  * next _ddebug object with a special case for the header line.
573  */
574 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
575 {
576         struct ddebug_iter *iter = m->private;
577         struct _ddebug *dp;
578
579         if (verbose)
580                 printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
581                         __func__, m, p, (unsigned long long)*pos);
582
583         if (p == SEQ_START_TOKEN)
584                 dp = ddebug_iter_first(iter);
585         else
586                 dp = ddebug_iter_next(iter);
587         ++*pos;
588         return dp;
589 }
590
591 /*
592  * Seq_ops show method.  Called several times within a read()
593  * call from userspace, with ddebug_lock held.  Formats the
594  * current _ddebug as a single human-readable line, with a
595  * special case for the header line.
596  */
597 static int ddebug_proc_show(struct seq_file *m, void *p)
598 {
599         struct ddebug_iter *iter = m->private;
600         struct _ddebug *dp = p;
601         char flagsbuf[8];
602
603         if (verbose)
604                 printk(KERN_INFO "%s: called m=%p p=%p\n",
605                         __func__, m, p);
606
607         if (p == SEQ_START_TOKEN) {
608                 seq_puts(m,
609                         "# filename:lineno [module]function flags format\n");
610                 return 0;
611         }
612
613         seq_printf(m, "%s:%u [%s]%s %s \"",
614                    dp->filename, dp->lineno,
615                    iter->table->mod_name, dp->function,
616                    ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
617         seq_escape(m, dp->format, "\t\r\n\"");
618         seq_puts(m, "\"\n");
619
620         return 0;
621 }
622
623 /*
624  * Seq_ops stop method.  Called at the end of each read()
625  * call from userspace.  Drops ddebug_lock.
626  */
627 static void ddebug_proc_stop(struct seq_file *m, void *p)
628 {
629         if (verbose)
630                 printk(KERN_INFO "%s: called m=%p p=%p\n",
631                         __func__, m, p);
632         mutex_unlock(&ddebug_lock);
633 }
634
635 static const struct seq_operations ddebug_proc_seqops = {
636         .start = ddebug_proc_start,
637         .next = ddebug_proc_next,
638         .show = ddebug_proc_show,
639         .stop = ddebug_proc_stop
640 };
641
642 /*
643  * File_ops->open method for <debugfs>/dynamic_debug/control.  Does the seq_file
644  * setup dance, and also creates an iterator to walk the _ddebugs.
645  * Note that we create a seq_file always, even for O_WRONLY files
646  * where it's not needed, as doing so simplifies the ->release method.
647  */
648 static int ddebug_proc_open(struct inode *inode, struct file *file)
649 {
650         struct ddebug_iter *iter;
651         int err;
652
653         if (verbose)
654                 printk(KERN_INFO "%s: called\n", __func__);
655
656         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
657         if (iter == NULL)
658                 return -ENOMEM;
659
660         err = seq_open(file, &ddebug_proc_seqops);
661         if (err) {
662                 kfree(iter);
663                 return err;
664         }
665         ((struct seq_file *) file->private_data)->private = iter;
666         return 0;
667 }
668
669 static const struct file_operations ddebug_proc_fops = {
670         .owner = THIS_MODULE,
671         .open = ddebug_proc_open,
672         .read = seq_read,
673         .llseek = seq_lseek,
674         .release = seq_release_private,
675         .write = ddebug_proc_write
676 };
677
678 /*
679  * Allocate a new ddebug_table for the given module
680  * and add it to the global list.
681  */
682 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
683                              const char *name)
684 {
685         struct ddebug_table *dt;
686         char *new_name;
687
688         dt = kzalloc(sizeof(*dt), GFP_KERNEL);
689         if (dt == NULL)
690                 return -ENOMEM;
691         new_name = kstrdup(name, GFP_KERNEL);
692         if (new_name == NULL) {
693                 kfree(dt);
694                 return -ENOMEM;
695         }
696         dt->mod_name = new_name;
697         dt->num_ddebugs = n;
698         dt->num_enabled = 0;
699         dt->ddebugs = tab;
700
701         mutex_lock(&ddebug_lock);
702         list_add_tail(&dt->link, &ddebug_tables);
703         mutex_unlock(&ddebug_lock);
704
705         if (verbose)
706                 printk(KERN_INFO "%u debug prints in module %s\n",
707                                  n, dt->mod_name);
708         return 0;
709 }
710 EXPORT_SYMBOL_GPL(ddebug_add_module);
711
712 static void ddebug_table_free(struct ddebug_table *dt)
713 {
714         list_del_init(&dt->link);
715         kfree(dt->mod_name);
716         kfree(dt);
717 }
718
719 /*
720  * Called in response to a module being unloaded.  Removes
721  * any ddebug_table's which point at the module.
722  */
723 int ddebug_remove_module(const char *mod_name)
724 {
725         struct ddebug_table *dt, *nextdt;
726         int ret = -ENOENT;
727
728         if (verbose)
729                 printk(KERN_INFO "%s: removing module \"%s\"\n",
730                                 __func__, mod_name);
731
732         mutex_lock(&ddebug_lock);
733         list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
734                 if (!strcmp(dt->mod_name, mod_name)) {
735                         ddebug_table_free(dt);
736                         ret = 0;
737                 }
738         }
739         mutex_unlock(&ddebug_lock);
740         return ret;
741 }
742 EXPORT_SYMBOL_GPL(ddebug_remove_module);
743
744 static void ddebug_remove_all_tables(void)
745 {
746         mutex_lock(&ddebug_lock);
747         while (!list_empty(&ddebug_tables)) {
748                 struct ddebug_table *dt = list_entry(ddebug_tables.next,
749                                                       struct ddebug_table,
750                                                       link);
751                 ddebug_table_free(dt);
752         }
753         mutex_unlock(&ddebug_lock);
754 }
755
756 static __initdata int ddebug_init_success;
757
758 static int __init dynamic_debug_init_debugfs(void)
759 {
760         struct dentry *dir, *file;
761
762         if (!ddebug_init_success)
763                 return -ENODEV;
764
765         dir = debugfs_create_dir("dynamic_debug", NULL);
766         if (!dir)
767                 return -ENOMEM;
768         file = debugfs_create_file("control", 0644, dir, NULL,
769                                         &ddebug_proc_fops);
770         if (!file) {
771                 debugfs_remove(dir);
772                 return -ENOMEM;
773         }
774         return 0;
775 }
776
777 static int __init dynamic_debug_init(void)
778 {
779         struct _ddebug *iter, *iter_start;
780         const char *modname = NULL;
781         int ret = 0;
782         int n = 0;
783
784         if (__start___verbose != __stop___verbose) {
785                 iter = __start___verbose;
786                 modname = iter->modname;
787                 iter_start = iter;
788                 for (; iter < __stop___verbose; iter++) {
789                         if (strcmp(modname, iter->modname)) {
790                                 ret = ddebug_add_module(iter_start, n, modname);
791                                 if (ret)
792                                         goto out_free;
793                                 n = 0;
794                                 modname = iter->modname;
795                                 iter_start = iter;
796                         }
797                         n++;
798                 }
799                 ret = ddebug_add_module(iter_start, n, modname);
800         }
801
802         /* ddebug_query boot param got passed -> set it up */
803         if (ddebug_setup_string[0] != '\0') {
804                 ret = ddebug_exec_query(ddebug_setup_string);
805                 if (ret)
806                         pr_warning("Invalid ddebug boot param %s",
807                                    ddebug_setup_string);
808                 else
809                         pr_info("ddebug initialized with string %s",
810                                 ddebug_setup_string);
811         }
812
813 out_free:
814         if (ret)
815                 ddebug_remove_all_tables();
816         else
817                 ddebug_init_success = 1;
818         return 0;
819 }
820 /* Allow early initialization for boot messages via boot param */
821 arch_initcall(dynamic_debug_init);
822 /* Debugfs setup must be done later */
823 module_init(dynamic_debug_init_debugfs);