1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Debug Implementation.
25 \*****************************************************************************/
27 #include <linux/kmod.h>
29 #include <linux/vmalloc.h>
30 #include <linux/pagemap.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/kthread.h>
34 #include <linux/hardirq.h>
35 #include <linux/interrupt.h>
36 #include <linux/spinlock.h>
37 #include <sys/sysmacros.h>
39 #include <sys/debug.h>
42 #ifdef DEBUG_SUBSYSTEM
43 #undef DEBUG_SUBSYSTEM
46 #define DEBUG_SUBSYSTEM S_DEBUG
48 unsigned long spl_debug_subsys = ~0;
49 EXPORT_SYMBOL(spl_debug_subsys);
50 module_param(spl_debug_subsys, long, 0644);
51 MODULE_PARM_DESC(spl_debug_subsys, "Subsystem debugging level mask.");
53 unsigned long spl_debug_mask = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE);
54 EXPORT_SYMBOL(spl_debug_mask);
55 module_param(spl_debug_mask, long, 0644);
56 MODULE_PARM_DESC(spl_debug_mask, "Debugging level mask.");
58 unsigned long spl_debug_printk = D_CANTMASK;
59 EXPORT_SYMBOL(spl_debug_printk);
60 module_param(spl_debug_printk, long, 0644);
61 MODULE_PARM_DESC(spl_debug_printk, "Console printk level mask.");
63 int spl_debug_mb = -1;
64 EXPORT_SYMBOL(spl_debug_mb);
65 module_param(spl_debug_mb, int, 0644);
66 MODULE_PARM_DESC(spl_debug_mb, "Total debug buffer size.");
68 unsigned int spl_debug_binary = 1;
69 EXPORT_SYMBOL(spl_debug_binary);
71 unsigned int spl_debug_catastrophe;
72 EXPORT_SYMBOL(spl_debug_catastrophe);
74 unsigned int spl_debug_panic_on_bug = 0;
75 EXPORT_SYMBOL(spl_debug_panic_on_bug);
76 module_param(spl_debug_panic_on_bug, int, 0644);
77 MODULE_PARM_DESC(spl_debug_panic_on_bug, "Panic on BUG");
79 static char spl_debug_file_name[PATH_MAX];
80 char spl_debug_file_path[PATH_MAX] = "/tmp/spl-log";
82 unsigned int spl_console_ratelimit = 1;
83 EXPORT_SYMBOL(spl_console_ratelimit);
85 long spl_console_max_delay;
86 EXPORT_SYMBOL(spl_console_max_delay);
88 long spl_console_min_delay;
89 EXPORT_SYMBOL(spl_console_min_delay);
91 unsigned int spl_console_backoff = SPL_DEFAULT_BACKOFF;
92 EXPORT_SYMBOL(spl_console_backoff);
94 unsigned int spl_debug_stack;
95 EXPORT_SYMBOL(spl_debug_stack);
97 static int spl_panic_in_progress;
99 union trace_data_union (*trace_data[TCD_TYPE_MAX])[NR_CPUS] __cacheline_aligned;
100 char *trace_console_buffers[NR_CPUS][3];
101 struct rw_semaphore trace_sem;
102 atomic_t trace_tage_allocated = ATOMIC_INIT(0);
104 static int spl_debug_dump_all_pages(dumplog_priv_t *dp, char *);
105 static void trace_fini(void);
108 /* Memory percentage breakdown by type */
109 static unsigned int pages_factor[TCD_TYPE_MAX] = {
110 80, /* 80% pages for TCD_TYPE_PROC */
111 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
112 10 /* 10% pages for TCD_TYPE_IRQ */
116 spl_debug_subsys2str(int subsys)
159 spl_debug_dbg2str(int debug)
186 spl_debug_mask2str(char *str, int size, unsigned long mask, int is_subsys)
188 const char *(*fn)(int bit) = is_subsys ? spl_debug_subsys2str :
193 if (mask == 0) { /* "0" */
197 } else { /* space-separated tokens */
198 for (i = 0; i < 32; i++) {
201 if ((mask & bit) == 0)
205 if (token == NULL) /* unused bit */
208 if (len > 0) { /* separator? */
214 while (*token != 0) {
223 /* terminate 'str' */
233 spl_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
235 const char *(*fn)(int bit) = is_subsys ? spl_debug_subsys2str :
240 /* match against known tokens */
241 for (i = 0; i < 32; i++) {
245 if (token == NULL) /* unused? */
250 if (j == len) { /* end of token */
261 if (str[j] == token[j])
264 if (str[j] < 'A' || 'Z' < str[j])
267 if (str[j] - 'A' + 'a' != token[j])
272 return -EINVAL; /* no match */
276 spl_debug_str2mask(unsigned long *mask, const char *str, int is_subsys)
279 int m = 0, matched, n, t;
281 /* Allow a number for backwards compatibility */
282 for (n = strlen(str); n > 0; n--)
283 if (!isspace(str[n-1]))
287 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 && matched == n) {
292 /* <str> must be a list of debug tokens or numbers separated by
293 * whitespace and optionally an operator ('+' or '-'). If an operator
294 * appears first in <str>, '*mask' is used as the starting point
295 * (relative), otherwise 0 is used (absolute). An operator applies to
296 * all following tokens up to the next operator. */
299 while (isspace(*str)) /* skip whitespace */
305 if (*str == '+' || *str == '-') {
308 /* op on first token == relative */
312 while (isspace(*str)) /* skip whitespace */
315 if (*str == 0) /* trailing op */
319 /* find token length */
320 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
323 if (spl_debug_token2mask(&t, str, n, is_subsys) != 0)
343 spl_debug_dumplog_internal(dumplog_priv_t *dp)
347 journal_info = current->journal_info;
348 current->journal_info = NULL;
350 snprintf(spl_debug_file_name, sizeof(spl_debug_file_path) - 1,
351 "%s.%ld.%ld", spl_debug_file_path,
352 get_seconds(), (long)dp->dp_pid);
353 printk("SPL: Dumping log to %s\n", spl_debug_file_name);
354 spl_debug_dump_all_pages(dp, spl_debug_file_name);
356 current->journal_info = journal_info;
360 spl_debug_dumplog_thread(void *arg)
362 dumplog_priv_t *dp = (dumplog_priv_t *)arg;
364 spl_debug_dumplog_internal(dp);
365 atomic_set(&dp->dp_done, 1);
366 wake_up(&dp->dp_waitq);
367 complete_and_exit(NULL, 0);
369 return 0; /* Unreachable */
372 /* When flag is set do not use a new thread for the debug dump */
374 spl_debug_dumplog(int flags)
376 struct task_struct *tsk;
379 init_waitqueue_head(&dp.dp_waitq);
380 dp.dp_pid = current->pid;
382 atomic_set(&dp.dp_done, 0);
384 if (dp.dp_flags & DL_NOTHREAD) {
385 spl_debug_dumplog_internal(&dp);
388 tsk = kthread_create(spl_debug_dumplog_thread,(void *)&dp,"spl_debug");
392 wake_up_process(tsk);
393 wait_event(dp.dp_waitq, atomic_read(&dp.dp_done));
398 EXPORT_SYMBOL(spl_debug_dumplog);
401 trace_get_console_buffer(void)
408 } else if (in_softirq()) {
414 return trace_console_buffers[cpu][idx];
418 trace_put_console_buffer(char *buffer)
424 trace_lock_tcd(struct trace_cpu_data *tcd)
426 __ASSERT(tcd->tcd_type < TCD_TYPE_MAX);
428 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
434 trace_unlock_tcd(struct trace_cpu_data *tcd)
436 __ASSERT(tcd->tcd_type < TCD_TYPE_MAX);
438 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
441 static struct trace_cpu_data *
445 struct trace_cpu_data *tcd;
449 tcd = &(*trace_data[TCD_TYPE_IRQ])[cpu].tcd;
450 else if (in_softirq())
451 tcd = &(*trace_data[TCD_TYPE_SOFTIRQ])[cpu].tcd;
453 tcd = &(*trace_data[TCD_TYPE_PROC])[cpu].tcd;
461 trace_put_tcd (struct trace_cpu_data *tcd)
463 trace_unlock_tcd(tcd);
469 trace_set_debug_header(struct spl_debug_header *header, int subsys,
470 int mask, const int line, unsigned long stack)
474 do_gettimeofday(&tv);
476 header->ph_subsys = subsys;
477 header->ph_mask = mask;
478 header->ph_cpu_id = smp_processor_id();
479 header->ph_sec = (__u32)tv.tv_sec;
480 header->ph_usec = tv.tv_usec;
481 header->ph_stack = stack;
482 header->ph_pid = current->pid;
483 header->ph_line_num = line;
489 trace_print_to_console(struct spl_debug_header *hdr, int mask, const char *buf,
490 int len, const char *file, const char *fn)
492 char *prefix = "SPL", *ptype = NULL;
494 if ((mask & D_EMERG) != 0) {
497 } else if ((mask & D_ERROR) != 0) {
500 } else if ((mask & D_WARNING) != 0) {
502 ptype = KERN_WARNING;
503 } else if ((mask & (D_CONSOLE | spl_debug_printk)) != 0) {
508 if ((mask & D_CONSOLE) != 0) {
509 printk("%s%s: %.*s", ptype, prefix, len, buf);
511 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix,
512 hdr->ph_pid, hdr->ph_stack, file,
513 hdr->ph_line_num, fn, len, buf);
520 trace_max_debug_mb(void)
522 return MAX(512, ((num_physpages >> (20 - PAGE_SHIFT)) * 80) / 100);
525 static struct trace_page *
529 struct trace_page *tage;
531 page = alloc_pages(gfp | __GFP_NOWARN, 0);
535 tage = kmalloc(sizeof(*tage), gfp);
537 __free_pages(page, 0);
542 atomic_inc(&trace_tage_allocated);
548 tage_free(struct trace_page *tage)
550 __ASSERT(tage != NULL);
551 __ASSERT(tage->page != NULL);
553 __free_pages(tage->page, 0);
555 atomic_dec(&trace_tage_allocated);
558 static struct trace_page *
559 tage_from_list(struct list_head *list)
561 return list_entry(list, struct trace_page, linkage);
565 tage_to_tail(struct trace_page *tage, struct list_head *queue)
567 __ASSERT(tage != NULL);
568 __ASSERT(queue != NULL);
570 list_move_tail(&tage->linkage, queue);
573 /* try to return a page that has 'len' bytes left at the end */
574 static struct trace_page *
575 trace_get_tage_try(struct trace_cpu_data *tcd, unsigned long len)
577 struct trace_page *tage;
579 if (tcd->tcd_cur_pages > 0) {
580 __ASSERT(!list_empty(&tcd->tcd_pages));
581 tage = tage_from_list(tcd->tcd_pages.prev);
582 if (tage->used + len <= PAGE_SIZE)
586 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
587 if (tcd->tcd_cur_stock_pages > 0) {
588 tage = tage_from_list(tcd->tcd_stock_pages.prev);
589 tcd->tcd_cur_stock_pages--;
590 list_del_init(&tage->linkage);
592 tage = tage_alloc(GFP_ATOMIC);
595 "failure to allocate a tage (%ld)\n",
602 tage->cpu = smp_processor_id();
603 tage->type = tcd->tcd_type;
604 list_add_tail(&tage->linkage, &tcd->tcd_pages);
605 tcd->tcd_cur_pages++;
613 /* return a page that has 'len' bytes left at the end */
614 static struct trace_page *
615 trace_get_tage(struct trace_cpu_data *tcd, unsigned long len)
617 struct trace_page *tage;
619 __ASSERT(len <= PAGE_SIZE);
621 tage = trace_get_tage_try(tcd, len);
625 if (tcd->tcd_cur_pages > 0) {
626 tage = tage_from_list(tcd->tcd_pages.next);
628 tage_to_tail(tage, &tcd->tcd_pages);
635 spl_debug_vmsg(spl_debug_limit_state_t *cdls, int subsys, int mask,
636 const char *file, const char *fn, const int line,
637 const char *format1, va_list args, const char *format2, ...)
639 struct trace_cpu_data *tcd = NULL;
640 struct spl_debug_header header = { 0, };
641 struct trace_page *tage;
642 /* string_buf is used only if tcd != NULL, and is always set then */
643 char *string_buf = NULL;
646 int needed = 85; /* average message length */
652 if (strchr(file, '/'))
653 file = strrchr(file, '/') + 1;
655 trace_set_debug_header(&header, subsys, mask, line, CDEBUG_STACK());
657 tcd = trace_get_tcd();
661 if (tcd->tcd_shutting_down) {
667 known_size = strlen(file) + 1;
669 known_size += strlen(fn) + 1;
671 if (spl_debug_binary)
672 known_size += sizeof(header);
674 /* '2' used because vsnprintf returns real size required for output
675 * _without_ terminating NULL. */
676 for (i = 0; i < 2; i++) {
677 tage = trace_get_tage(tcd, needed + known_size + 1);
679 if (needed + known_size > PAGE_SIZE)
687 string_buf = (char *)page_address(tage->page) +
688 tage->used + known_size;
690 max_nob = PAGE_SIZE - tage->used - known_size;
692 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
702 needed = vsnprintf(string_buf, max_nob, format1, ap);
707 remain = max_nob - needed;
711 va_start(ap, format2);
712 needed += vsnprintf(string_buf+needed, remain, format2, ap);
716 if (needed < max_nob)
720 header.ph_len = known_size + needed;
721 debug_buf = (char *)page_address(tage->page) + tage->used;
723 if (spl_debug_binary) {
724 memcpy(debug_buf, &header, sizeof(header));
725 tage->used += sizeof(header);
726 debug_buf += sizeof(header);
729 strcpy(debug_buf, file);
730 tage->used += strlen(file) + 1;
731 debug_buf += strlen(file) + 1;
734 strcpy(debug_buf, fn);
735 tage->used += strlen(fn) + 1;
736 debug_buf += strlen(fn) + 1;
739 __ASSERT(debug_buf == string_buf);
741 tage->used += needed;
742 __ASSERT (tage->used <= PAGE_SIZE);
745 if ((mask & spl_debug_printk) == 0) {
746 /* no console output requested */
753 if (spl_console_ratelimit && cdls->cdls_next != 0 &&
754 !time_before(cdls->cdls_next, jiffies)) {
755 /* skipping a console message */
762 if (time_before(cdls->cdls_next + spl_console_max_delay +
763 (10 * HZ), jiffies)) {
764 /* last timeout was a long time ago */
765 cdls->cdls_delay /= spl_console_backoff * 4;
767 cdls->cdls_delay *= spl_console_backoff;
769 if (cdls->cdls_delay < spl_console_min_delay)
770 cdls->cdls_delay = spl_console_min_delay;
771 else if (cdls->cdls_delay > spl_console_max_delay)
772 cdls->cdls_delay = spl_console_max_delay;
775 /* ensure cdls_next is never zero after it's been seen */
776 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
780 trace_print_to_console(&header, mask, string_buf, needed, file, fn);
783 string_buf = trace_get_console_buffer();
786 if (format1 != NULL) {
788 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
791 if (format2 != NULL) {
792 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
794 va_start(ap, format2);
795 needed += vsnprintf(string_buf+needed, remain, format2, ap);
799 trace_print_to_console(&header, mask,
800 string_buf, needed, file, fn);
802 trace_put_console_buffer(string_buf);
805 if (cdls != NULL && cdls->cdls_count != 0) {
806 string_buf = trace_get_console_buffer();
808 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
809 "Skipped %d previous similar message%s\n",
810 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
812 trace_print_to_console(&header, mask,
813 string_buf, needed, file, fn);
815 trace_put_console_buffer(string_buf);
816 cdls->cdls_count = 0;
821 EXPORT_SYMBOL(spl_debug_vmsg);
823 /* Do the collect_pages job on a single CPU: assumes that all other
824 * CPUs have been stopped during a panic. If this isn't true for
825 * some arch, this will have to be implemented separately in each arch.
828 collect_pages_from_single_cpu(struct page_collection *pc)
830 struct trace_cpu_data *tcd;
833 tcd_for_each(tcd, i, j) {
834 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
835 tcd->tcd_cur_pages = 0;
840 collect_pages_on_all_cpus(struct page_collection *pc)
842 struct trace_cpu_data *tcd;
845 spin_lock(&pc->pc_lock);
846 for_each_possible_cpu(cpu) {
847 tcd_for_each_type_lock(tcd, i, cpu) {
848 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
849 tcd->tcd_cur_pages = 0;
852 spin_unlock(&pc->pc_lock);
856 collect_pages(dumplog_priv_t *dp, struct page_collection *pc)
858 INIT_LIST_HEAD(&pc->pc_pages);
860 if (spl_panic_in_progress || dp->dp_flags & DL_SINGLE_CPU)
861 collect_pages_from_single_cpu(pc);
863 collect_pages_on_all_cpus(pc);
867 put_pages_back_on_all_cpus(struct page_collection *pc)
869 struct trace_cpu_data *tcd;
870 struct list_head *cur_head;
871 struct trace_page *tage;
872 struct trace_page *tmp;
875 spin_lock(&pc->pc_lock);
877 for_each_possible_cpu(cpu) {
878 tcd_for_each_type_lock(tcd, i, cpu) {
879 cur_head = tcd->tcd_pages.next;
881 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
884 __ASSERT_TAGE_INVARIANT(tage);
886 if (tage->cpu != cpu || tage->type != i)
889 tage_to_tail(tage, cur_head);
890 tcd->tcd_cur_pages++;
895 spin_unlock(&pc->pc_lock);
899 put_pages_back(struct page_collection *pc)
901 if (!spl_panic_in_progress)
902 put_pages_back_on_all_cpus(pc);
906 trace_filp_open (const char *name, int flags, int mode, int *err)
908 struct file *filp = NULL;
911 filp = filp_open(name, flags, mode);
914 printk(KERN_ERR "SPL: Can't open %s file: %d\n", name, rc);
922 #define trace_filp_write(fp, b, s, p) (fp)->f_op->write((fp), (b), (s), p)
923 #define trace_filp_fsync(fp) (fp)->f_op->fsync((fp),(fp)->f_dentry,1)
924 #define trace_filp_close(f) filp_close(f, NULL)
925 #define trace_filp_poff(f) (&(f)->f_pos)
928 spl_debug_dump_all_pages(dumplog_priv_t *dp, char *filename)
930 struct page_collection pc;
932 struct trace_page *tage;
933 struct trace_page *tmp;
937 down_write(&trace_sem);
939 filp = trace_filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE,
943 printk(KERN_ERR "SPL: Can't open %s for dump: %d\n",
948 spin_lock_init(&pc.pc_lock);
949 collect_pages(dp, &pc);
950 if (list_empty(&pc.pc_pages)) {
958 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
959 __ASSERT_TAGE_INVARIANT(tage);
961 rc = trace_filp_write(filp, page_address(tage->page),
962 tage->used, trace_filp_poff(filp));
963 if (rc != (int)tage->used) {
964 printk(KERN_WARNING "SPL: Wanted to write %u "
965 "but wrote %d\n", tage->used, rc);
967 __ASSERT(list_empty(&pc.pc_pages));
970 list_del(&tage->linkage);
976 rc = trace_filp_fsync(filp);
978 printk(KERN_ERR "SPL: Unable to sync: %d\n", rc);
980 trace_filp_close(filp);
982 up_write(&trace_sem);
988 spl_debug_flush_pages(void)
991 struct page_collection pc;
992 struct trace_page *tage;
993 struct trace_page *tmp;
995 spin_lock_init(&pc.pc_lock);
996 init_waitqueue_head(&dp.dp_waitq);
997 dp.dp_pid = current->pid;
999 atomic_set(&dp.dp_done, 0);
1001 collect_pages(&dp, &pc);
1002 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1003 __ASSERT_TAGE_INVARIANT(tage);
1004 list_del(&tage->linkage);
1010 spl_debug_set_mask(unsigned long mask) {
1011 spl_debug_mask = mask;
1014 EXPORT_SYMBOL(spl_debug_set_mask);
1017 spl_debug_get_mask(void) {
1018 return spl_debug_mask;
1020 EXPORT_SYMBOL(spl_debug_get_mask);
1023 spl_debug_set_subsys(unsigned long subsys) {
1024 spl_debug_subsys = subsys;
1027 EXPORT_SYMBOL(spl_debug_set_subsys);
1030 spl_debug_get_subsys(void) {
1031 return spl_debug_subsys;
1033 EXPORT_SYMBOL(spl_debug_get_subsys);
1036 spl_debug_set_mb(int mb)
1039 int limit = trace_max_debug_mb();
1040 struct trace_cpu_data *tcd;
1042 if (mb < num_possible_cpus()) {
1043 printk(KERN_ERR "SPL: Refusing to set debug buffer size to "
1044 "%dMB - lower limit is %d\n", mb, num_possible_cpus());
1049 printk(KERN_ERR "SPL: Refusing to set debug buffer size to "
1050 "%dMB - upper limit is %d\n", mb, limit);
1054 mb /= num_possible_cpus();
1055 pages = mb << (20 - PAGE_SHIFT);
1057 down_write(&trace_sem);
1059 tcd_for_each(tcd, i, j)
1060 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1062 up_write(&trace_sem);
1066 EXPORT_SYMBOL(spl_debug_set_mb);
1069 spl_debug_get_mb(void)
1072 struct trace_cpu_data *tcd;
1073 int total_pages = 0;
1075 down_read(&trace_sem);
1077 tcd_for_each(tcd, i, j)
1078 total_pages += tcd->tcd_max_pages;
1080 up_read(&trace_sem);
1082 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1084 EXPORT_SYMBOL(spl_debug_get_mb);
1086 void spl_debug_dumpstack(struct task_struct *tsk)
1088 extern void show_task(struct task_struct *);
1093 printk("SPL: Showing stack for process %d\n", tsk->pid);
1096 EXPORT_SYMBOL(spl_debug_dumpstack);
1098 void spl_debug_bug(char *file, const char *func, const int line, int flags)
1100 spl_debug_catastrophe = 1;
1101 spl_debug_msg(NULL, 0, D_EMERG, file, func, line, "SBUG\n");
1103 if (in_interrupt()) {
1104 panic("SBUG in interrupt.\n");
1108 if (in_atomic() || irqs_disabled())
1109 flags |= DL_NOTHREAD;
1111 /* Ensure all debug pages and dumped by current cpu */
1112 if (spl_debug_panic_on_bug)
1113 spl_panic_in_progress = 1;
1115 spl_debug_dumpstack(NULL);
1116 spl_debug_dumplog(flags);
1118 if (spl_debug_panic_on_bug)
1121 set_task_state(current, TASK_UNINTERRUPTIBLE);
1125 EXPORT_SYMBOL(spl_debug_bug);
1128 spl_debug_clear_buffer(void)
1130 spl_debug_flush_pages();
1133 EXPORT_SYMBOL(spl_debug_clear_buffer);
1136 spl_debug_mark_buffer(char *text)
1138 CDEBUG(D_WARNING, "*************************************\n");
1139 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
1140 CDEBUG(D_WARNING, "*************************************\n");
1144 EXPORT_SYMBOL(spl_debug_mark_buffer);
1147 trace_init(int max_pages)
1149 struct trace_cpu_data *tcd;
1152 init_rwsem(&trace_sem);
1154 /* initialize trace_data */
1155 memset(trace_data, 0, sizeof(trace_data));
1156 for (i = 0; i < TCD_TYPE_MAX; i++) {
1157 trace_data[i] = kmalloc(sizeof(union trace_data_union) *
1158 NR_CPUS, GFP_KERNEL);
1159 if (trace_data[i] == NULL)
1163 tcd_for_each(tcd, i, j) {
1164 spin_lock_init(&tcd->tcd_lock);
1165 tcd->tcd_pages_factor = pages_factor[i];
1168 INIT_LIST_HEAD(&tcd->tcd_pages);
1169 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1170 tcd->tcd_cur_pages = 0;
1171 tcd->tcd_cur_stock_pages = 0;
1172 tcd->tcd_max_pages = (max_pages * pages_factor[i]) / 100;
1173 tcd->tcd_shutting_down = 0;
1176 for (i = 0; i < num_possible_cpus(); i++) {
1177 for (j = 0; j < 3; j++) {
1178 trace_console_buffers[i][j] =
1179 kmalloc(TRACE_CONSOLE_BUFFER_SIZE,
1182 if (trace_console_buffers[i][j] == NULL)
1190 printk(KERN_ERR "SPL: Insufficient memory for debug logs\n");
1197 int rc, max = spl_debug_mb;
1199 spl_console_max_delay = SPL_DEFAULT_MAX_DELAY;
1200 spl_console_min_delay = SPL_DEFAULT_MIN_DELAY;
1202 /* If spl_debug_mb is set to an invalid value or uninitialized
1203 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1204 if (max > (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5 ||
1205 max >= 512 || max < 0) {
1206 max = TCD_MAX_PAGES;
1208 max = (max / num_online_cpus()) << (20 - PAGE_SHIFT);
1211 rc = trace_init(max);
1219 trace_cleanup_on_all_cpus(void)
1221 struct trace_cpu_data *tcd;
1222 struct trace_page *tage;
1223 struct trace_page *tmp;
1226 for_each_possible_cpu(cpu) {
1227 tcd_for_each_type_lock(tcd, i, cpu) {
1228 tcd->tcd_shutting_down = 1;
1230 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1232 __ASSERT_TAGE_INVARIANT(tage);
1234 list_del(&tage->linkage);
1237 tcd->tcd_cur_pages = 0;
1247 trace_cleanup_on_all_cpus();
1249 for (i = 0; i < num_possible_cpus(); i++) {
1250 for (j = 0; j < 3; j++) {
1251 if (trace_console_buffers[i][j] != NULL) {
1252 kfree(trace_console_buffers[i][j]);
1253 trace_console_buffers[i][j] = NULL;
1258 for (i = 0; i < TCD_TYPE_MAX && trace_data[i] != NULL; i++) {
1259 kfree(trace_data[i]);
1260 trace_data[i] = NULL;