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