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