Whamcloud - gitweb
LU-9859 libcfs: add "default" keyword for debug mask
[fs/lustre-release.git] / libcfs / libcfs / debug.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * libcfs/libcfs/debug.c
32  *
33  * Author: Phil Schwan <phil@clusterfs.com>
34  *
35  */
36
37 # define DEBUG_SUBSYSTEM S_LNET
38
39 #include <linux/module.h>
40 #include <linux/ctype.h>
41 #include <libcfs/libcfs_string.h>
42 #include <linux/kthread.h>
43 #include <linux/stacktrace.h>
44 #include <linux/utsname.h>
45 #include <linux/kallsyms.h>
46 #ifdef HAVE_PANIC_NOTIFIER_H
47 #include <linux/panic_notifier.h>
48 #endif
49 #include "tracefile.h"
50
51 static char debug_file_name[1024];
52
53 unsigned int libcfs_subsystem_debug = LIBCFS_S_DEFAULT;
54 EXPORT_SYMBOL(libcfs_subsystem_debug);
55 module_param(libcfs_subsystem_debug, int, 0644);
56 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
57
58 unsigned int libcfs_debug = LIBCFS_D_DEFAULT;
59 EXPORT_SYMBOL(libcfs_debug);
60 module_param(libcfs_debug, int, 0644);
61 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
62
63 static int libcfs_param_debug_mb_set(const char *val,
64                                      cfs_kernel_param_arg_t *kp)
65 {
66         int rc;
67         unsigned int num;
68
69         rc = kstrtouint(val, 0, &num);
70         if (rc < 0)
71                 return rc;
72
73         num = cfs_trace_set_debug_mb(num);
74
75         *((unsigned int *)kp->arg) = num;
76         num = cfs_trace_get_debug_mb();
77         if (num)
78                 /* This value is more precise */
79                 *((unsigned int *)kp->arg) = num;
80
81         return 0;
82 }
83
84 /* While debug_mb setting look like unsigned int, in fact
85  * it needs quite a bunch of extra processing, so we define special
86  * debug_mb parameter type with corresponding methods to handle this case
87  */
88 static const struct kernel_param_ops param_ops_debug_mb = {
89         .set = libcfs_param_debug_mb_set,
90         .get = param_get_uint,
91 };
92
93 #define param_check_debug_mb(name, p) \
94                 __param_check(name, p, unsigned int)
95
96 static unsigned int libcfs_debug_mb;
97 #ifdef HAVE_KERNEL_PARAM_OPS
98 module_param(libcfs_debug_mb, debug_mb, 0644);
99 #else
100 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
101                   &param_ops_debug_mb, 0644);
102 #endif
103 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
104
105 unsigned int libcfs_printk = D_CANTMASK;
106 module_param(libcfs_printk, uint, 0644);
107 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
108
109 unsigned int libcfs_console_ratelimit = 1;
110 module_param(libcfs_console_ratelimit, uint, 0644);
111 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
112
113 static int param_set_delay_minmax(const char *val,
114                                   cfs_kernel_param_arg_t *kp,
115                                   long min, long max)
116 {
117         long d;
118         int sec;
119         int rc;
120
121         rc = kstrtoint(val, 0, &sec);
122         if (rc)
123                 return -EINVAL;
124
125         /* The sysfs setting is in centiseconds */
126         d = cfs_time_seconds(sec) / 100;
127         if (d < min || d > max)
128                 return -EINVAL;
129
130         *((unsigned int *)kp->arg) = d;
131
132         return 0;
133 }
134
135 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
136 {
137         unsigned int d = *(unsigned int *)kp->arg;
138
139         param_get_byte(buffer, kp);
140         return sprintf(buffer, "%lu%c", jiffies_to_msecs(d * 10) / MSEC_PER_SEC,
141                        strnchr(buffer, PAGE_SIZE, '\n') ? '\n' : '\0');
142 }
143
144 unsigned int libcfs_console_max_delay;
145 unsigned int libcfs_console_min_delay;
146
147 static int param_set_console_max_delay(const char *val,
148                                        cfs_kernel_param_arg_t *kp)
149 {
150         return param_set_delay_minmax(val, kp,
151                                       libcfs_console_min_delay, INT_MAX);
152 }
153
154 static const struct kernel_param_ops param_ops_console_max_delay = {
155         .set = param_set_console_max_delay,
156         .get = param_get_delay,
157 };
158
159 #define param_check_console_max_delay(name, p) \
160                 __param_check(name, p, unsigned int)
161
162 #ifdef HAVE_KERNEL_PARAM_OPS
163 module_param(libcfs_console_max_delay, console_max_delay, 0644);
164 #else
165 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
166                   param_get_delay, &param_ops_console_max_delay, 0644);
167 #endif
168 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
169
170 static int param_set_console_min_delay(const char *val,
171                                        cfs_kernel_param_arg_t *kp)
172 {
173         return param_set_delay_minmax(val, kp,
174                                       1, libcfs_console_max_delay);
175 }
176
177 static const struct kernel_param_ops param_ops_console_min_delay = {
178         .set = param_set_console_min_delay,
179         .get = param_get_delay,
180 };
181
182 #define param_check_console_min_delay(name, p) \
183                 __param_check(name, p, unsigned int)
184
185 #ifdef HAVE_KERNEL_PARAM_OPS
186 module_param(libcfs_console_min_delay, console_min_delay, 0644);
187 #else
188 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
189                   param_get_delay, &param_ops_console_min_delay, 0644);
190 #endif
191 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
192
193 #ifndef HAVE_PARAM_SET_UINT_MINMAX
194 static int param_set_uint_minmax(const char *val,
195                                  cfs_kernel_param_arg_t *kp,
196                                  unsigned int min, unsigned int max)
197 {
198         unsigned int num;
199         int ret;
200
201         if (!val)
202                 return -EINVAL;
203
204         ret = kstrtouint(val, 0, &num);
205         if (ret < 0 || num < min || num > max)
206                 return -EINVAL;
207
208         *((unsigned int *)kp->arg) = num;
209         return 0;
210 }
211 #endif
212
213 static int param_set_uintpos(const char *val,
214                              cfs_kernel_param_arg_t *kp)
215 {
216         return param_set_uint_minmax(val, kp, 1, -1);
217 }
218
219 static const struct kernel_param_ops param_ops_uintpos = {
220         .set = param_set_uintpos,
221         .get = param_get_uint,
222 };
223
224 #define param_check_uintpos(name, p) \
225                 __param_check(name, p, unsigned int)
226
227 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
228 #ifdef HAVE_KERNEL_PARAM_OPS
229 module_param(libcfs_console_backoff, uintpos, 0644);
230 #else
231 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
232                   &param_ops_uintpos, 0644);
233 #endif
234 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
235
236 unsigned int libcfs_debug_binary = 1;
237
238 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
239 EXPORT_SYMBOL(libcfs_stack);
240
241 unsigned int libcfs_catastrophe;
242 EXPORT_SYMBOL(libcfs_catastrophe);
243
244 unsigned int libcfs_watchdog_ratelimit = 300;
245 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
246
247 unsigned int libcfs_panic_on_lbug = 1;
248 module_param(libcfs_panic_on_lbug, uint, 0644);
249 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
250
251 atomic64_t libcfs_kmem = ATOMIC64_INIT(0);
252 EXPORT_SYMBOL(libcfs_kmem);
253
254 static DECLARE_COMPLETION(debug_complete);
255
256 /* We need to pass a pointer here, but elsewhere this must be a const */
257 char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
258 EXPORT_SYMBOL(libcfs_debug_file_path);
259 module_param(libcfs_debug_file_path, charp, 0644);
260 MODULE_PARM_DESC(libcfs_debug_file_path,
261                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
262
263 int libcfs_panic_in_progress;
264
265 /* libcfs_debug_token2mask() expects the returned string in lower-case */
266 static const char *libcfs_debug_subsys2str(int subsys)
267 {
268         static const char *const libcfs_debug_subsystems[] =
269                 LIBCFS_DEBUG_SUBSYS_NAMES;
270
271         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
272                 return NULL;
273
274         return libcfs_debug_subsystems[subsys];
275 }
276
277 /* libcfs_debug_token2mask() expects the returned string in lower-case */
278 static const char *libcfs_debug_dbg2str(int debug)
279 {
280         static const char * const libcfs_debug_masks[] =
281                 LIBCFS_DEBUG_MASKS_NAMES;
282
283         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
284                 return NULL;
285
286         return libcfs_debug_masks[debug];
287 }
288
289 int
290 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
291 {
292         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
293                                                  libcfs_debug_dbg2str;
294         int len = 0;
295         const char *token;
296         int i;
297
298         if (mask == 0) {                        /* "0" */
299                 if (size > 0)
300                         str[0] = '0';
301                 len = 1;
302         } else {                                /* space-separated tokens */
303                 for (i = 0; i < 32; i++) {
304                         if ((mask & BIT(i)) == 0)
305                                 continue;
306
307                         token = fn(i);
308                         if (!token)     /* unused bit */
309                                 continue;
310
311                         if (len > 0) {          /* separator? */
312                                 if (len < size)
313                                         str[len] = ' ';
314                                 len++;
315                         }
316
317                         while (*token != 0) {
318                                 if (len < size)
319                                         str[len] = *token;
320                                 token++;
321                                 len++;
322                         }
323                 }
324         }
325
326         /* terminate 'str' */
327         if (len < size)
328                 str[len] = 0;
329         else
330                 str[size - 1] = 0;
331
332         return len;
333 }
334
335 int
336 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
337 {
338         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
339                                                  libcfs_debug_dbg2str;
340         int m = 0;
341         int matched;
342         int n;
343         int t;
344
345         /* Allow a number for backwards compatibility */
346         for (n = strlen(str); n > 0; n--)
347                 if (!isspace(str[n-1]))
348                         break;
349         matched = n;
350         t = sscanf(str, "%i%n", &m, &matched);
351         if (t >= 1 && matched == n) {
352                 /* don't print warning for lctl set_param debug=0 or -1 */
353                 if (m != 0 && m != -1)
354                         CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
355                 *mask = m;
356                 return 0;
357         }
358
359         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK, ~0,
360                             is_subsys ? LIBCFS_S_DEFAULT : LIBCFS_D_DEFAULT);
361 }
362
363 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
364
365 /* Upcall function once a Lustre log has been dumped.
366  *
367  * @file        path of the dumped log
368  */
369 static void libcfs_run_debug_log_upcall(char *file)
370 {
371         char *argv[3];
372         int rc;
373         static const char * const envp[] = {
374                 "HOME=/",
375                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
376                 NULL
377         };
378
379         ENTRY;
380         argv[0] = lnet_debug_log_upcall;
381
382         LASSERTF(file, "called on a null filename\n");
383         argv[1] = file; /* only need to pass the path of the file */
384
385         argv[2] = NULL;
386
387         rc = call_usermodehelper(argv[0], argv, (char **)envp, 1);
388         if (rc < 0 && rc != -ENOENT) {
389                 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
390                        rc, argv[0], argv[1]);
391         } else {
392                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
393                        argv[0], argv[1]);
394         }
395 }
396
397 /**
398  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
399  */
400 static void libcfs_debug_dumplog_internal(void *arg)
401 {
402         static time64_t last_dump_time;
403         time64_t current_time;
404
405         current_time = ktime_get_real_seconds();
406
407         if (strncmp(libcfs_debug_file_path, "NONE", 4) != 0 &&
408             current_time > last_dump_time) {
409                 last_dump_time = current_time;
410                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
411                          "%s.%lld.%ld", libcfs_debug_file_path,
412                          (s64)current_time, (uintptr_t)arg);
413                 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
414                 cfs_tracefile_dump_all_pages(debug_file_name);
415                 libcfs_run_debug_log_upcall(debug_file_name);
416         }
417 }
418
419 static int libcfs_debug_dumplog_thread(void *arg)
420 {
421         libcfs_debug_dumplog_internal(arg);
422         complete(&debug_complete);
423         return 0;
424 }
425
426 static DEFINE_MUTEX(libcfs_debug_dumplog_lock);
427
428 void libcfs_debug_dumplog(void)
429 {
430         struct task_struct *dumper;
431
432         ENTRY;
433
434         if (mutex_trylock(&libcfs_debug_dumplog_lock) == 0)
435                 return;
436
437         /* If a previous call was interrupted, debug_complete->done
438          * might be elevated, and so we won't actually wait here.
439          * So we reinit the completion to ensure we wait for
440          * one thread to complete, though it might not be the one
441          * we start if there are overlaping thread.
442          */
443         reinit_completion(&debug_complete);
444         dumper = kthread_run(libcfs_debug_dumplog_thread,
445                              (void *)(long)current->pid,
446                              "libcfs_debug_dumper");
447         if (IS_ERR(dumper))
448                 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
449                        PTR_ERR(dumper));
450         else
451                 wait_for_completion_interruptible(&debug_complete);
452
453         mutex_unlock(&libcfs_debug_dumplog_lock);
454 }
455 EXPORT_SYMBOL(libcfs_debug_dumplog);
456
457 /* coverity[+kill] */
458 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
459 {
460         libcfs_catastrophe = 1;
461         libcfs_debug_msg(msgdata, "LBUG\n");
462
463         if (in_interrupt()) {
464                 panic("LBUG in interrupt.\n");
465                 /* not reached */
466         }
467
468         libcfs_debug_dumpstack(NULL);
469         if (libcfs_panic_on_lbug)
470                 panic("LBUG");
471         else
472                 libcfs_debug_dumplog();
473         set_current_state(TASK_UNINTERRUPTIBLE);
474         while (1)
475                 schedule();
476 }
477 EXPORT_SYMBOL(lbug_with_loc);
478
479 #ifdef CONFIG_STACKTRACE
480
481 #ifndef HAVE_SAVE_STACK_TRACE_TSK
482 #define save_stack_trace_tsk(tsk, trace)                                       \
483 do {                                                                           \
484         if (tsk == current)                                                    \
485                 save_stack_trace(trace);                                       \
486         else                                                                   \
487                 pr_info("No stack, save_stack_trace_tsk() not exported\n");    \
488 } while (0)
489 #endif
490
491 static void cfs_print_stack_trace(unsigned long *entries, unsigned int nr)
492 {
493         unsigned int i;
494
495         /* Prefer %pB for backtraced symbolic names since it was added in:
496          * Linux v2.6.38-6557-g0f77a8d37825
497          * vsprintf: Introduce %pB format specifier
498          */
499         for (i = 0; i < nr; i++)
500                 pr_info("[<0>] %pB\n", (void *)entries[i]);
501 }
502
503 #define MAX_ST_ENTRIES  100
504 static DEFINE_SPINLOCK(st_lock);
505
506 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
507  * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
508  * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
509  */
510 #ifdef CONFIG_ARCH_STACKWALK
511 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
512                                               unsigned long *store,
513                                               unsigned int size,
514                                               unsigned int skipnr);
515 static stack_trace_save_tsk_t *task_dump_stack;
516 #endif
517
518 void __init cfs_debug_init(void)
519 {
520 #ifdef CONFIG_ARCH_STACKWALK
521         task_dump_stack = (void *)
522                         cfs_kallsyms_lookup_name("stack_trace_save_tsk");
523
524 #endif
525 }
526
527 static void libcfs_call_trace(struct task_struct *tsk)
528 {
529         static unsigned long entries[MAX_ST_ENTRIES];
530 #ifdef CONFIG_ARCH_STACKWALK
531         unsigned int nr_entries;
532
533         spin_lock(&st_lock);
534         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
535                 init_utsname()->release, init_utsname()->version);
536         pr_info("Call Trace TBD:\n");
537         if (task_dump_stack) {
538                 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
539                 cfs_print_stack_trace(entries, nr_entries);
540         }
541         spin_unlock(&st_lock);
542 #else
543         struct stack_trace trace;
544
545         trace.nr_entries = 0;
546         trace.max_entries = MAX_ST_ENTRIES;
547         trace.entries = entries;
548         trace.skip = 0;
549
550         spin_lock(&st_lock);
551         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
552                 init_utsname()->release, init_utsname()->version);
553         pr_info("Call Trace:\n");
554         save_stack_trace_tsk(tsk, &trace);
555         cfs_print_stack_trace(trace.entries, trace.nr_entries);
556         spin_unlock(&st_lock);
557 #endif
558 }
559
560 #else /* !CONFIG_STACKTRACE */
561
562 #ifdef CONFIG_X86
563 #include <linux/nmi.h>
564 #include <asm/stacktrace.h>
565
566 #ifdef HAVE_STACKTRACE_OPS
567 static int print_trace_stack(void *data, char *name)
568 {
569         printk(" <%s> ", name);
570         return 0;
571 }
572
573 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
574 static int
575 #else
576 static void
577 #endif
578 print_trace_address(void *data, unsigned long addr, int reliable)
579 {
580         char fmt[32];
581
582         touch_nmi_watchdog();
583         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "" : "? ");
584         __print_symbol(fmt, addr);
585 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
586         return 0;
587 #endif
588 }
589
590 static const struct stacktrace_ops print_trace_ops = {
591         .stack          = print_trace_stack,
592         .address        = print_trace_address,
593         .walk_stack     = print_context_stack,
594 };
595 #endif /* HAVE_STACKTRACE_OPS */
596
597 static void libcfs_call_trace(struct task_struct *tsk)
598 {
599 #ifdef HAVE_STACKTRACE_OPS
600         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
601         printk("\nCall Trace:\n");
602         dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
603         printk("\n");
604 #else /* !HAVE_STACKTRACE_OPS */
605         if (tsk == current)
606                 dump_stack();
607         else
608                 CWARN("can't show stack: kernel doesn't export show_task\n");
609 #endif /* HAVE_STACKTRACE_OPS */
610 }
611
612 #else /* !CONFIG_X86 */
613
614 static void libcfs_call_trace(struct task_struct *tsk)
615 {
616         if (tsk == current)
617                 dump_stack();
618         else
619                 CWARN("can't show stack: kernel doesn't export show_task\n");
620 }
621
622 #endif /* CONFIG_X86 */
623
624 #endif /* CONFIG_STACKTRACE */
625
626 void libcfs_debug_dumpstack(struct task_struct *tsk)
627 {
628         libcfs_call_trace(tsk ?: current);
629 }
630 EXPORT_SYMBOL(libcfs_debug_dumpstack);
631
632 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
633                           void *unused2)
634 {
635         if (libcfs_panic_in_progress)
636                 return 0;
637
638         libcfs_panic_in_progress = 1;
639         mb();
640
641 #ifdef LNET_DUMP_ON_PANIC
642         /* This is currently disabled because it spews far too much to the
643          * console on the rare cases it is ever triggered. */
644
645         if (in_interrupt()) {
646                 cfs_trace_debug_print();
647         } else {
648                 libcfs_debug_dumplog_internal((void *)(long)current->pid);
649         }
650 #endif
651         return 0;
652 }
653
654 static struct notifier_block libcfs_panic_notifier = {
655         .notifier_call          = panic_notifier,
656         .next                   = NULL,
657         .priority               = 10000,
658 };
659
660 static void libcfs_register_panic_notifier(void)
661 {
662         atomic_notifier_chain_register(&panic_notifier_list,
663                                        &libcfs_panic_notifier);
664 }
665
666 static void libcfs_unregister_panic_notifier(void)
667 {
668         atomic_notifier_chain_unregister(&panic_notifier_list,
669                                          &libcfs_panic_notifier);
670 }
671
672 int libcfs_debug_init(unsigned long bufsize)
673 {
674         unsigned int max = libcfs_debug_mb;
675         int rc = 0;
676
677         if (libcfs_console_max_delay <= 0 || /* not set by user or */
678             libcfs_console_min_delay <= 0 || /* set to invalid values */
679             libcfs_console_min_delay >= libcfs_console_max_delay) {
680                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
681                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
682         }
683
684         /* If libcfs_debug_mb is uninitialized then just make the
685          * total buffers smp_num_cpus * TCD_MAX_PAGES
686          */
687         if (max < num_possible_cpus()) {
688                 max = TCD_MAX_PAGES;
689         } else {
690                 max = (max / num_possible_cpus());
691                 max <<= (20 - PAGE_SHIFT);
692         }
693
694         rc = cfs_tracefile_init(max);
695         if (rc)
696                 return rc;
697
698         libcfs_register_panic_notifier();
699         kernel_param_lock(THIS_MODULE);
700         if (libcfs_debug_mb == 0)
701                 libcfs_debug_mb = cfs_trace_get_debug_mb();
702         kernel_param_unlock(THIS_MODULE);
703         return rc;
704 }
705
706 int libcfs_debug_cleanup(void)
707 {
708         libcfs_unregister_panic_notifier();
709         kernel_param_lock(THIS_MODULE);
710         cfs_tracefile_exit();
711         kernel_param_unlock(THIS_MODULE);
712         return 0;
713 }
714
715 int libcfs_debug_clear_buffer(void)
716 {
717         cfs_trace_flush_pages();
718         return 0;
719 }
720
721 /* Debug markers, although printed by S_LNET should not be be marked as such. */
722 #undef DEBUG_SUBSYSTEM
723 #define DEBUG_SUBSYSTEM S_UNDEFINED
724 int libcfs_debug_mark_buffer(const char *text)
725 {
726         CDEBUG(D_TRACE,
727                "**************************************************\n");
728         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
729         CDEBUG(D_TRACE,
730                "**************************************************\n");
731
732         return 0;
733 }
734
735 #undef DEBUG_SUBSYSTEM
736 #define DEBUG_SUBSYSTEM S_LNET