2 * This file is part of the SPL: Solaris Porting Layer.
4 * This file was originally part of Lustre, http://www.lustre.org.
5 * but has subsequently been adapted for use in the SPL in
6 * accordance with the GPL.
8 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
10 * Produced at Lawrence Livermore National Laboratory
12 * Zach Brown <zab@clusterfs.com>
13 * Phil Schwan <phil@clusterfs.com>
14 * Brian Behlendorf <behlendorf1@llnl.gov>,
15 * Herb Wartens <wartens2@llnl.gov>,
16 * Jim Garlick <garlick@llnl.gov>
19 * This is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This is distributed in the hope that it will be useful, but WITHOUT
25 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include <linux/kmod.h>
36 #include <linux/vmalloc.h>
37 #include <linux/pagemap.h>
38 #include <linux/slab.h>
39 #include <linux/ctype.h>
40 #include <linux/kthread.h>
41 #include <linux/hardirq.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <sys/sysmacros.h>
46 #include <sys/debug.h>
49 #ifdef DEBUG_SUBSYSTEM
50 #undef DEBUG_SUBSYSTEM
53 #define DEBUG_SUBSYSTEM S_DEBUG
55 unsigned long spl_debug_subsys
= ~0;
56 EXPORT_SYMBOL(spl_debug_subsys
);
57 module_param(spl_debug_subsys
, long, 0644);
58 MODULE_PARM_DESC(spl_debug_subsys
, "Subsystem debugging level mask.");
60 unsigned long spl_debug_mask
= (D_EMERG
| D_ERROR
| D_WARNING
| D_CONSOLE
);
61 EXPORT_SYMBOL(spl_debug_mask
);
62 module_param(spl_debug_mask
, long, 0644);
63 MODULE_PARM_DESC(spl_debug_mask
, "Debugging level mask.");
65 unsigned long spl_debug_printk
= D_CANTMASK
;
66 EXPORT_SYMBOL(spl_debug_printk
);
67 module_param(spl_debug_printk
, long, 0644);
68 MODULE_PARM_DESC(spl_debug_printk
, "Console printk level mask.");
70 int spl_debug_mb
= -1;
71 EXPORT_SYMBOL(spl_debug_mb
);
72 module_param(spl_debug_mb
, int, 0644);
73 MODULE_PARM_DESC(spl_debug_mb
, "Total debug buffer size.");
75 unsigned int spl_debug_binary
= 1;
76 EXPORT_SYMBOL(spl_debug_binary
);
78 unsigned int spl_debug_catastrophe
;
79 EXPORT_SYMBOL(spl_debug_catastrophe
);
81 unsigned int spl_debug_panic_on_bug
= 1;
82 EXPORT_SYMBOL(spl_debug_panic_on_bug
);
83 module_param(spl_debug_panic_on_bug
, int, 0644);
84 MODULE_PARM_DESC(spl_debug_panic_on_bug
, "Panic on BUG");
86 static char spl_debug_file_name
[PATH_MAX
];
87 char spl_debug_file_path
[PATH_MAX
] = "/var/dumps/spl-log";
89 unsigned int spl_console_ratelimit
= 1;
90 EXPORT_SYMBOL(spl_console_ratelimit
);
92 long spl_console_max_delay
;
93 EXPORT_SYMBOL(spl_console_max_delay
);
95 long spl_console_min_delay
;
96 EXPORT_SYMBOL(spl_console_min_delay
);
98 unsigned int spl_console_backoff
= SPL_DEFAULT_BACKOFF
;
99 EXPORT_SYMBOL(spl_console_backoff
);
101 unsigned int spl_debug_stack
;
102 EXPORT_SYMBOL(spl_debug_stack
);
104 static int spl_panic_in_progress
;
106 union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
] __cacheline_aligned
;
107 char *trace_console_buffers
[NR_CPUS
][3];
108 struct rw_semaphore trace_sem
;
109 atomic_t trace_tage_allocated
= ATOMIC_INIT(0);
111 static int spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *);
112 static void trace_fini(void);
115 /* Memory percentage breakdown by type */
116 static unsigned int pages_factor
[TCD_TYPE_MAX
] = {
117 80, /* 80% pages for TCD_TYPE_PROC */
118 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
119 10 /* 10% pages for TCD_TYPE_IRQ */
123 spl_debug_subsys2str(int subsys
)
166 spl_debug_dbg2str(int debug
)
193 spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
)
195 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
200 if (mask
== 0) { /* "0" */
204 } else { /* space-separated tokens */
205 for (i
= 0; i
< 32; i
++) {
208 if ((mask
& bit
) == 0)
212 if (token
== NULL
) /* unused bit */
215 if (len
> 0) { /* separator? */
221 while (*token
!= 0) {
230 /* terminate 'str' */
240 spl_debug_token2mask(int *mask
, const char *str
, int len
, int is_subsys
)
242 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
247 /* match against known tokens */
248 for (i
= 0; i
< 32; i
++) {
252 if (token
== NULL
) /* unused? */
257 if (j
== len
) { /* end of token */
268 if (str
[j
] == token
[j
])
271 if (str
[j
] < 'A' || 'Z' < str
[j
])
274 if (str
[j
] - 'A' + 'a' != token
[j
])
279 return -EINVAL
; /* no match */
283 spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
)
286 int m
= 0, matched
, n
, t
;
288 /* Allow a number for backwards compatibility */
289 for (n
= strlen(str
); n
> 0; n
--)
290 if (!isspace(str
[n
-1]))
294 if ((t
= sscanf(str
, "%i%n", &m
, &matched
)) >= 1 && matched
== n
) {
299 /* <str> must be a list of debug tokens or numbers separated by
300 * whitespace and optionally an operator ('+' or '-'). If an operator
301 * appears first in <str>, '*mask' is used as the starting point
302 * (relative), otherwise 0 is used (absolute). An operator applies to
303 * all following tokens up to the next operator. */
306 while (isspace(*str
)) /* skip whitespace */
312 if (*str
== '+' || *str
== '-') {
315 /* op on first token == relative */
319 while (isspace(*str
)) /* skip whitespace */
322 if (*str
== 0) /* trailing op */
326 /* find token length */
327 for (n
= 0; str
[n
] != 0 && !isspace(str
[n
]); n
++);
330 if (spl_debug_token2mask(&t
, str
, n
, is_subsys
) != 0)
350 spl_debug_dumplog_internal(dumplog_priv_t
*dp
)
354 journal_info
= current
->journal_info
;
355 current
->journal_info
= NULL
;
357 snprintf(spl_debug_file_name
, sizeof(spl_debug_file_path
) - 1,
358 "%s.%ld.%ld", spl_debug_file_path
,
359 get_seconds(), (long)dp
->dp_pid
);
360 printk(KERN_ALERT
"SPL: dumping log to %s\n", spl_debug_file_name
);
361 spl_debug_dump_all_pages(dp
, spl_debug_file_name
);
363 current
->journal_info
= journal_info
;
367 spl_debug_dumplog_thread(void *arg
)
369 dumplog_priv_t
*dp
= (dumplog_priv_t
*)arg
;
371 spl_debug_dumplog_internal(dp
);
372 atomic_set(&dp
->dp_done
, 1);
373 wake_up(&dp
->dp_waitq
);
374 complete_and_exit(NULL
, 0);
376 return 0; /* Unreachable */
379 /* When flag is set do not use a new thread for the debug dump */
381 spl_debug_dumplog(int flags
)
383 struct task_struct
*tsk
;
386 init_waitqueue_head(&dp
.dp_waitq
);
387 dp
.dp_pid
= current
->pid
;
389 atomic_set(&dp
.dp_done
, 0);
391 if (dp
.dp_flags
& DL_NOTHREAD
) {
392 spl_debug_dumplog_internal(&dp
);
395 tsk
= kthread_create(spl_debug_dumplog_thread
,(void *)&dp
,"spl_debug");
399 wake_up_process(tsk
);
400 wait_event(dp
.dp_waitq
, atomic_read(&dp
.dp_done
));
405 EXPORT_SYMBOL(spl_debug_dumplog
);
408 trace_get_console_buffer(void)
415 } else if (in_softirq()) {
421 return trace_console_buffers
[cpu
][idx
];
425 trace_put_console_buffer(char *buffer
)
431 trace_lock_tcd(struct trace_cpu_data
*tcd
)
433 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
435 spin_lock_irqsave(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
441 trace_unlock_tcd(struct trace_cpu_data
*tcd
)
443 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
445 spin_unlock_irqrestore(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
448 static struct trace_cpu_data
*
452 struct trace_cpu_data
*tcd
;
456 tcd
= &(*trace_data
[TCD_TYPE_IRQ
])[cpu
].tcd
;
457 else if (in_softirq())
458 tcd
= &(*trace_data
[TCD_TYPE_SOFTIRQ
])[cpu
].tcd
;
460 tcd
= &(*trace_data
[TCD_TYPE_PROC
])[cpu
].tcd
;
468 trace_put_tcd (struct trace_cpu_data
*tcd
)
470 trace_unlock_tcd(tcd
);
476 trace_set_debug_header(struct spl_debug_header
*header
, int subsys
,
477 int mask
, const int line
, unsigned long stack
)
481 do_gettimeofday(&tv
);
483 header
->ph_subsys
= subsys
;
484 header
->ph_mask
= mask
;
485 header
->ph_cpu_id
= smp_processor_id();
486 header
->ph_sec
= (__u32
)tv
.tv_sec
;
487 header
->ph_usec
= tv
.tv_usec
;
488 header
->ph_stack
= stack
;
489 header
->ph_pid
= current
->pid
;
490 header
->ph_line_num
= line
;
496 trace_print_to_console(struct spl_debug_header
*hdr
, int mask
, const char *buf
,
497 int len
, const char *file
, const char *fn
)
499 char *prefix
= "SPL", *ptype
= NULL
;
501 if ((mask
& D_EMERG
) != 0) {
504 } else if ((mask
& D_ERROR
) != 0) {
507 } else if ((mask
& D_WARNING
) != 0) {
509 ptype
= KERN_WARNING
;
510 } else if ((mask
& (D_CONSOLE
| spl_debug_printk
)) != 0) {
515 if ((mask
& D_CONSOLE
) != 0) {
516 printk("%s%s: %.*s", ptype
, prefix
, len
, buf
);
518 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype
, prefix
,
519 hdr
->ph_pid
, hdr
->ph_stack
, file
,
520 hdr
->ph_line_num
, fn
, len
, buf
);
527 trace_max_debug_mb(void)
529 return MAX(512, ((num_physpages
>> (20 - PAGE_SHIFT
)) * 80) / 100);
532 static struct trace_page
*
536 struct trace_page
*tage
;
538 page
= alloc_pages(gfp
| __GFP_NOWARN
, 0);
542 tage
= kmalloc(sizeof(*tage
), gfp
);
544 __free_pages(page
, 0);
549 atomic_inc(&trace_tage_allocated
);
555 tage_free(struct trace_page
*tage
)
557 __ASSERT(tage
!= NULL
);
558 __ASSERT(tage
->page
!= NULL
);
560 __free_pages(tage
->page
, 0);
562 atomic_dec(&trace_tage_allocated
);
565 static struct trace_page
*
566 tage_from_list(struct list_head
*list
)
568 return list_entry(list
, struct trace_page
, linkage
);
572 tage_to_tail(struct trace_page
*tage
, struct list_head
*queue
)
574 __ASSERT(tage
!= NULL
);
575 __ASSERT(queue
!= NULL
);
577 list_move_tail(&tage
->linkage
, queue
);
580 /* try to return a page that has 'len' bytes left at the end */
581 static struct trace_page
*
582 trace_get_tage_try(struct trace_cpu_data
*tcd
, unsigned long len
)
584 struct trace_page
*tage
;
586 if (tcd
->tcd_cur_pages
> 0) {
587 __ASSERT(!list_empty(&tcd
->tcd_pages
));
588 tage
= tage_from_list(tcd
->tcd_pages
.prev
);
589 if (tage
->used
+ len
<= PAGE_SIZE
)
593 if (tcd
->tcd_cur_pages
< tcd
->tcd_max_pages
) {
594 if (tcd
->tcd_cur_stock_pages
> 0) {
595 tage
= tage_from_list(tcd
->tcd_stock_pages
.prev
);
596 tcd
->tcd_cur_stock_pages
--;
597 list_del_init(&tage
->linkage
);
599 tage
= tage_alloc(GFP_ATOMIC
);
602 "failure to allocate a tage (%ld)\n",
609 tage
->cpu
= smp_processor_id();
610 tage
->type
= tcd
->tcd_type
;
611 list_add_tail(&tage
->linkage
, &tcd
->tcd_pages
);
612 tcd
->tcd_cur_pages
++;
620 /* return a page that has 'len' bytes left at the end */
621 static struct trace_page
*
622 trace_get_tage(struct trace_cpu_data
*tcd
, unsigned long len
)
624 struct trace_page
*tage
;
626 __ASSERT(len
<= PAGE_SIZE
);
628 tage
= trace_get_tage_try(tcd
, len
);
632 if (tcd
->tcd_cur_pages
> 0) {
633 tage
= tage_from_list(tcd
->tcd_pages
.next
);
635 tage_to_tail(tage
, &tcd
->tcd_pages
);
642 spl_debug_vmsg(spl_debug_limit_state_t
*cdls
, int subsys
, int mask
,
643 const char *file
, const char *fn
, const int line
,
644 const char *format1
, va_list args
, const char *format2
, ...)
646 struct trace_cpu_data
*tcd
= NULL
;
647 struct spl_debug_header header
= { 0, };
648 struct trace_page
*tage
;
649 /* string_buf is used only if tcd != NULL, and is always set then */
650 char *string_buf
= NULL
;
653 int needed
= 85; /* average message length */
659 if (strchr(file
, '/'))
660 file
= strrchr(file
, '/') + 1;
662 trace_set_debug_header(&header
, subsys
, mask
, line
, CDEBUG_STACK());
664 tcd
= trace_get_tcd();
668 if (tcd
->tcd_shutting_down
) {
674 known_size
= strlen(file
) + 1;
676 known_size
+= strlen(fn
) + 1;
678 if (spl_debug_binary
)
679 known_size
+= sizeof(header
);
681 /* '2' used because vsnprintf returns real size required for output
682 * _without_ terminating NULL. */
683 for (i
= 0; i
< 2; i
++) {
684 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
686 if (needed
+ known_size
> PAGE_SIZE
)
694 string_buf
= (char *)page_address(tage
->page
) +
695 tage
->used
+ known_size
;
697 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
699 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
709 needed
= vsnprintf(string_buf
, max_nob
, format1
, ap
);
714 remain
= max_nob
- needed
;
718 va_start(ap
, format2
);
719 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
723 if (needed
< max_nob
)
727 header
.ph_len
= known_size
+ needed
;
728 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
730 if (spl_debug_binary
) {
731 memcpy(debug_buf
, &header
, sizeof(header
));
732 tage
->used
+= sizeof(header
);
733 debug_buf
+= sizeof(header
);
736 strcpy(debug_buf
, file
);
737 tage
->used
+= strlen(file
) + 1;
738 debug_buf
+= strlen(file
) + 1;
741 strcpy(debug_buf
, fn
);
742 tage
->used
+= strlen(fn
) + 1;
743 debug_buf
+= strlen(fn
) + 1;
746 __ASSERT(debug_buf
== string_buf
);
748 tage
->used
+= needed
;
749 __ASSERT (tage
->used
<= PAGE_SIZE
);
752 if ((mask
& spl_debug_printk
) == 0) {
753 /* no console output requested */
760 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
761 !time_before(cdls
->cdls_next
, jiffies
)) {
762 /* skipping a console message */
769 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
770 (10 * HZ
), jiffies
)) {
771 /* last timeout was a long time ago */
772 cdls
->cdls_delay
/= spl_console_backoff
* 4;
774 cdls
->cdls_delay
*= spl_console_backoff
;
776 if (cdls
->cdls_delay
< spl_console_min_delay
)
777 cdls
->cdls_delay
= spl_console_min_delay
;
778 else if (cdls
->cdls_delay
> spl_console_max_delay
)
779 cdls
->cdls_delay
= spl_console_max_delay
;
782 /* ensure cdls_next is never zero after it's been seen */
783 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
787 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
790 string_buf
= trace_get_console_buffer();
793 if (format1
!= NULL
) {
795 needed
= vsnprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
, format1
, ap
);
798 if (format2
!= NULL
) {
799 remain
= TRACE_CONSOLE_BUFFER_SIZE
- needed
;
801 va_start(ap
, format2
);
802 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
806 trace_print_to_console(&header
, mask
,
807 string_buf
, needed
, file
, fn
);
809 trace_put_console_buffer(string_buf
);
812 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
813 string_buf
= trace_get_console_buffer();
815 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
816 "Skipped %d previous similar message%s\n",
817 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
819 trace_print_to_console(&header
, mask
,
820 string_buf
, needed
, file
, fn
);
822 trace_put_console_buffer(string_buf
);
823 cdls
->cdls_count
= 0;
828 EXPORT_SYMBOL(spl_debug_vmsg
);
830 /* Do the collect_pages job on a single CPU: assumes that all other
831 * CPUs have been stopped during a panic. If this isn't true for
832 * some arch, this will have to be implemented separately in each arch.
835 collect_pages_from_single_cpu(struct page_collection
*pc
)
837 struct trace_cpu_data
*tcd
;
840 tcd_for_each(tcd
, i
, j
) {
841 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
842 tcd
->tcd_cur_pages
= 0;
847 collect_pages_on_all_cpus(struct page_collection
*pc
)
849 struct trace_cpu_data
*tcd
;
852 spin_lock(&pc
->pc_lock
);
853 for_each_possible_cpu(cpu
) {
854 tcd_for_each_type_lock(tcd
, i
, cpu
) {
855 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
856 tcd
->tcd_cur_pages
= 0;
859 spin_unlock(&pc
->pc_lock
);
863 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
865 INIT_LIST_HEAD(&pc
->pc_pages
);
867 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
868 collect_pages_from_single_cpu(pc
);
870 collect_pages_on_all_cpus(pc
);
874 put_pages_back_on_all_cpus(struct page_collection
*pc
)
876 struct trace_cpu_data
*tcd
;
877 struct list_head
*cur_head
;
878 struct trace_page
*tage
;
879 struct trace_page
*tmp
;
882 spin_lock(&pc
->pc_lock
);
884 for_each_possible_cpu(cpu
) {
885 tcd_for_each_type_lock(tcd
, i
, cpu
) {
886 cur_head
= tcd
->tcd_pages
.next
;
888 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
891 __ASSERT_TAGE_INVARIANT(tage
);
893 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
896 tage_to_tail(tage
, cur_head
);
897 tcd
->tcd_cur_pages
++;
902 spin_unlock(&pc
->pc_lock
);
906 put_pages_back(struct page_collection
*pc
)
908 if (!spl_panic_in_progress
)
909 put_pages_back_on_all_cpus(pc
);
913 trace_filp_open (const char *name
, int flags
, int mode
, int *err
)
915 struct file
*filp
= NULL
;
918 filp
= filp_open(name
, flags
, mode
);
921 printk(KERN_ERR
"SPL: Can't open %s file: %d\n", name
, rc
);
929 #define trace_filp_write(fp, b, s, p) (fp)->f_op->write((fp), (b), (s), p)
930 #define trace_filp_fsync(fp) (fp)->f_op->fsync((fp),(fp)->f_dentry,1)
931 #define trace_filp_close(f) filp_close(f, NULL)
932 #define trace_filp_poff(f) (&(f)->f_pos)
935 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
937 struct page_collection pc
;
939 struct trace_page
*tage
;
940 struct trace_page
*tmp
;
944 down_write(&trace_sem
);
946 filp
= trace_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
950 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
955 spin_lock_init(&pc
.pc_lock
);
956 collect_pages(dp
, &pc
);
957 if (list_empty(&pc
.pc_pages
)) {
965 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
966 __ASSERT_TAGE_INVARIANT(tage
);
968 rc
= trace_filp_write(filp
, page_address(tage
->page
),
969 tage
->used
, trace_filp_poff(filp
));
970 if (rc
!= (int)tage
->used
) {
971 printk(KERN_WARNING
"SPL: Wanted to write %u "
972 "but wrote %d\n", tage
->used
, rc
);
974 __ASSERT(list_empty(&pc
.pc_pages
));
977 list_del(&tage
->linkage
);
983 rc
= trace_filp_fsync(filp
);
985 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
987 trace_filp_close(filp
);
989 up_write(&trace_sem
);
995 spl_debug_flush_pages(void)
998 struct page_collection pc
;
999 struct trace_page
*tage
;
1000 struct trace_page
*tmp
;
1002 spin_lock_init(&pc
.pc_lock
);
1003 init_waitqueue_head(&dp
.dp_waitq
);
1004 dp
.dp_pid
= current
->pid
;
1006 atomic_set(&dp
.dp_done
, 0);
1008 collect_pages(&dp
, &pc
);
1009 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
1010 __ASSERT_TAGE_INVARIANT(tage
);
1011 list_del(&tage
->linkage
);
1017 spl_debug_set_mask(unsigned long mask
) {
1018 spl_debug_mask
= mask
;
1021 EXPORT_SYMBOL(spl_debug_set_mask
);
1024 spl_debug_get_mask(void) {
1025 return spl_debug_mask
;
1027 EXPORT_SYMBOL(spl_debug_get_mask
);
1030 spl_debug_set_subsys(unsigned long subsys
) {
1031 spl_debug_subsys
= subsys
;
1034 EXPORT_SYMBOL(spl_debug_set_subsys
);
1037 spl_debug_get_subsys(void) {
1038 return spl_debug_subsys
;
1040 EXPORT_SYMBOL(spl_debug_get_subsys
);
1043 spl_debug_set_mb(int mb
)
1046 int limit
= trace_max_debug_mb();
1047 struct trace_cpu_data
*tcd
;
1049 if (mb
< num_possible_cpus()) {
1050 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1051 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1056 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1057 "%dMB - upper limit is %d\n", mb
, limit
);
1061 mb
/= num_possible_cpus();
1062 pages
= mb
<< (20 - PAGE_SHIFT
);
1064 down_write(&trace_sem
);
1066 tcd_for_each(tcd
, i
, j
)
1067 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1069 up_write(&trace_sem
);
1073 EXPORT_SYMBOL(spl_debug_set_mb
);
1076 spl_debug_get_mb(void)
1079 struct trace_cpu_data
*tcd
;
1080 int total_pages
= 0;
1082 down_read(&trace_sem
);
1084 tcd_for_each(tcd
, i
, j
)
1085 total_pages
+= tcd
->tcd_max_pages
;
1087 up_read(&trace_sem
);
1089 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1091 EXPORT_SYMBOL(spl_debug_get_mb
);
1093 void spl_debug_dumpstack(struct task_struct
*tsk
)
1095 extern void show_task(struct task_struct
*);
1100 printk(KERN_ERR
"SPL: Showing stack for process %d\n", tsk
->pid
);
1103 EXPORT_SYMBOL(spl_debug_dumpstack
);
1105 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1107 spl_debug_catastrophe
= 1;
1108 spl_debug_msg(NULL
, 0, D_EMERG
, file
, func
, line
, "SBUG\n");
1110 if (in_interrupt()) {
1111 panic("SBUG in interrupt.\n");
1115 /* Ensure all debug pages and dumped by current cpu */
1116 if (spl_debug_panic_on_bug
)
1117 spl_panic_in_progress
= 1;
1119 spl_debug_dumpstack(NULL
);
1120 spl_debug_dumplog(flags
);
1122 if (spl_debug_panic_on_bug
)
1125 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1129 EXPORT_SYMBOL(spl_debug_bug
);
1132 spl_debug_clear_buffer(void)
1134 spl_debug_flush_pages();
1137 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1140 spl_debug_mark_buffer(char *text
)
1142 CDEBUG(D_WARNING
, "*************************************\n");
1143 CDEBUG(D_WARNING
, "DEBUG MARKER: %s\n", text
);
1144 CDEBUG(D_WARNING
, "*************************************\n");
1148 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1151 trace_init(int max_pages
)
1153 struct trace_cpu_data
*tcd
;
1156 init_rwsem(&trace_sem
);
1158 /* initialize trace_data */
1159 memset(trace_data
, 0, sizeof(trace_data
));
1160 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1161 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1162 NR_CPUS
, GFP_KERNEL
);
1163 if (trace_data
[i
] == NULL
)
1167 tcd_for_each(tcd
, i
, j
) {
1168 spin_lock_init(&tcd
->tcd_lock
);
1169 tcd
->tcd_pages_factor
= pages_factor
[i
];
1172 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1173 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1174 tcd
->tcd_cur_pages
= 0;
1175 tcd
->tcd_cur_stock_pages
= 0;
1176 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1177 tcd
->tcd_shutting_down
= 0;
1180 for (i
= 0; i
< num_possible_cpus(); i
++) {
1181 for (j
= 0; j
< 3; j
++) {
1182 trace_console_buffers
[i
][j
] =
1183 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1186 if (trace_console_buffers
[i
][j
] == NULL
)
1194 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1201 int rc
, max
= spl_debug_mb
;
1203 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1204 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1206 /* If spl_debug_mb is set to an invalid value or uninitialized
1207 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1208 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1209 max
>= 512 || max
< 0) {
1210 max
= TCD_MAX_PAGES
;
1212 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1215 rc
= trace_init(max
);
1223 trace_cleanup_on_all_cpus(void)
1225 struct trace_cpu_data
*tcd
;
1226 struct trace_page
*tage
;
1227 struct trace_page
*tmp
;
1230 for_each_possible_cpu(cpu
) {
1231 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1232 tcd
->tcd_shutting_down
= 1;
1234 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1236 __ASSERT_TAGE_INVARIANT(tage
);
1238 list_del(&tage
->linkage
);
1241 tcd
->tcd_cur_pages
= 0;
1251 trace_cleanup_on_all_cpus();
1253 for (i
= 0; i
< num_possible_cpus(); i
++) {
1254 for (j
= 0; j
< 3; j
++) {
1255 if (trace_console_buffers
[i
][j
] != NULL
) {
1256 kfree(trace_console_buffers
[i
][j
]);
1257 trace_console_buffers
[i
][j
] = NULL
;
1262 for (i
= 0; i
< TCD_TYPE_MAX
&& trace_data
[i
] != NULL
; i
++) {
1263 kfree(trace_data
[i
]);
1264 trace_data
[i
] = NULL
;