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 <linux/proc_compat.h>
38 #include <linux/file_compat.h>
39 #include <sys/sysmacros.h>
40 #include <spl-debug.h>
41 #include <spl-trace.h>
44 #ifdef SS_DEBUG_SUBSYS
45 #undef SS_DEBUG_SUBSYS
48 #define SS_DEBUG_SUBSYS SS_DEBUG
50 unsigned long spl_debug_subsys
= ~0;
51 EXPORT_SYMBOL(spl_debug_subsys
);
52 module_param(spl_debug_subsys
, ulong
, 0644);
53 MODULE_PARM_DESC(spl_debug_subsys
, "Subsystem debugging level mask.");
55 unsigned long spl_debug_mask
= SD_CANTMASK
;
56 EXPORT_SYMBOL(spl_debug_mask
);
57 module_param(spl_debug_mask
, ulong
, 0644);
58 MODULE_PARM_DESC(spl_debug_mask
, "Debugging level mask.");
60 unsigned long spl_debug_printk
= SD_CANTMASK
;
61 EXPORT_SYMBOL(spl_debug_printk
);
62 module_param(spl_debug_printk
, ulong
, 0644);
63 MODULE_PARM_DESC(spl_debug_printk
, "Console printk level mask.");
65 int spl_debug_mb
= -1;
66 EXPORT_SYMBOL(spl_debug_mb
);
67 module_param(spl_debug_mb
, int, 0644);
68 MODULE_PARM_DESC(spl_debug_mb
, "Total debug buffer size.");
70 unsigned int spl_debug_binary
= 1;
71 EXPORT_SYMBOL(spl_debug_binary
);
73 unsigned int spl_debug_catastrophe
;
74 EXPORT_SYMBOL(spl_debug_catastrophe
);
76 unsigned int spl_debug_panic_on_bug
= 0;
77 EXPORT_SYMBOL(spl_debug_panic_on_bug
);
78 module_param(spl_debug_panic_on_bug
, uint
, 0644);
79 MODULE_PARM_DESC(spl_debug_panic_on_bug
, "Panic on BUG");
81 static char spl_debug_file_name
[PATH_MAX
];
82 char spl_debug_file_path
[PATH_MAX
] = "/tmp/spl-log";
84 unsigned int spl_console_ratelimit
= 1;
85 EXPORT_SYMBOL(spl_console_ratelimit
);
87 long spl_console_max_delay
;
88 EXPORT_SYMBOL(spl_console_max_delay
);
90 long spl_console_min_delay
;
91 EXPORT_SYMBOL(spl_console_min_delay
);
93 unsigned int spl_console_backoff
= SPL_DEFAULT_BACKOFF
;
94 EXPORT_SYMBOL(spl_console_backoff
);
96 unsigned int spl_debug_stack
;
97 EXPORT_SYMBOL(spl_debug_stack
);
99 static int spl_panic_in_progress
;
101 union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
] __cacheline_aligned
;
102 char *trace_console_buffers
[NR_CPUS
][3];
103 struct rw_semaphore trace_sem
;
104 atomic_t trace_tage_allocated
= ATOMIC_INIT(0);
106 static int spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *);
107 static void trace_fini(void);
110 /* Memory percentage breakdown by type */
111 static unsigned int pages_factor
[TCD_TYPE_MAX
] = {
112 80, /* 80% pages for TCD_TYPE_PROC */
113 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
114 10 /* 10% pages for TCD_TYPE_IRQ */
118 spl_debug_subsys2str(int subsys
)
185 spl_debug_dbg2str(int debug
)
212 spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
)
214 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
219 if (mask
== 0) { /* "0" */
223 } else { /* space-separated tokens */
224 for (i
= 0; i
< 32; i
++) {
227 if ((mask
& bit
) == 0)
231 if (token
== NULL
) /* unused bit */
234 if (len
> 0) { /* separator? */
240 while (*token
!= 0) {
249 /* terminate 'str' */
259 spl_debug_token2mask(int *mask
, const char *str
, int len
, int is_subsys
)
261 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
266 /* match against known tokens */
267 for (i
= 0; i
< 32; i
++) {
271 if (token
== NULL
) /* unused? */
276 if (j
== len
) { /* end of token */
287 if (str
[j
] == token
[j
])
290 if (str
[j
] < 'A' || 'Z' < str
[j
])
293 if (str
[j
] - 'A' + 'a' != token
[j
])
298 return -EINVAL
; /* no match */
302 spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
)
305 int m
= 0, matched
, n
, t
;
307 /* Allow a number for backwards compatibility */
308 for (n
= strlen(str
); n
> 0; n
--)
309 if (!isspace(str
[n
-1]))
313 if ((t
= sscanf(str
, "%i%n", &m
, &matched
)) >= 1 && matched
== n
) {
318 /* <str> must be a list of debug tokens or numbers separated by
319 * whitespace and optionally an operator ('+' or '-'). If an operator
320 * appears first in <str>, '*mask' is used as the starting point
321 * (relative), otherwise 0 is used (absolute). An operator applies to
322 * all following tokens up to the next operator. */
325 while (isspace(*str
)) /* skip whitespace */
331 if (*str
== '+' || *str
== '-') {
334 /* op on first token == relative */
338 while (isspace(*str
)) /* skip whitespace */
341 if (*str
== 0) /* trailing op */
345 /* find token length */
346 for (n
= 0; str
[n
] != 0 && !isspace(str
[n
]); n
++);
349 if (spl_debug_token2mask(&t
, str
, n
, is_subsys
) != 0)
369 spl_debug_dumplog_internal(dumplog_priv_t
*dp
)
373 journal_info
= current
->journal_info
;
374 current
->journal_info
= NULL
;
376 snprintf(spl_debug_file_name
, sizeof(spl_debug_file_path
) - 1,
377 "%s.%ld.%ld", spl_debug_file_path
,
378 get_seconds(), (long)dp
->dp_pid
);
379 printk("SPL: Dumping log to %s\n", spl_debug_file_name
);
380 spl_debug_dump_all_pages(dp
, spl_debug_file_name
);
382 current
->journal_info
= journal_info
;
386 spl_debug_dumplog_thread(void *arg
)
388 dumplog_priv_t
*dp
= (dumplog_priv_t
*)arg
;
390 spl_debug_dumplog_internal(dp
);
391 atomic_set(&dp
->dp_done
, 1);
392 wake_up(&dp
->dp_waitq
);
393 complete_and_exit(NULL
, 0);
395 return 0; /* Unreachable */
398 /* When flag is set do not use a new thread for the debug dump */
400 spl_debug_dumplog(int flags
)
402 struct task_struct
*tsk
;
405 init_waitqueue_head(&dp
.dp_waitq
);
406 dp
.dp_pid
= current
->pid
;
408 atomic_set(&dp
.dp_done
, 0);
410 if (dp
.dp_flags
& DL_NOTHREAD
) {
411 spl_debug_dumplog_internal(&dp
);
414 tsk
= kthread_create(spl_debug_dumplog_thread
,(void *)&dp
,"spl_debug");
418 wake_up_process(tsk
);
419 wait_event(dp
.dp_waitq
, atomic_read(&dp
.dp_done
));
424 EXPORT_SYMBOL(spl_debug_dumplog
);
427 trace_get_console_buffer(void)
434 } else if (in_softirq()) {
440 return trace_console_buffers
[cpu
][idx
];
444 trace_put_console_buffer(char *buffer
)
450 trace_lock_tcd(struct trace_cpu_data
*tcd
)
452 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
454 spin_lock_irqsave(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
460 trace_unlock_tcd(struct trace_cpu_data
*tcd
)
462 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
464 spin_unlock_irqrestore(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
467 static struct trace_cpu_data
*
471 struct trace_cpu_data
*tcd
;
475 tcd
= &(*trace_data
[TCD_TYPE_IRQ
])[cpu
].tcd
;
476 else if (in_softirq())
477 tcd
= &(*trace_data
[TCD_TYPE_SOFTIRQ
])[cpu
].tcd
;
479 tcd
= &(*trace_data
[TCD_TYPE_PROC
])[cpu
].tcd
;
487 trace_put_tcd (struct trace_cpu_data
*tcd
)
489 trace_unlock_tcd(tcd
);
495 trace_set_debug_header(struct spl_debug_header
*header
, int subsys
,
496 int mask
, const int line
, unsigned long stack
)
500 do_gettimeofday(&tv
);
502 header
->ph_subsys
= subsys
;
503 header
->ph_mask
= mask
;
504 header
->ph_cpu_id
= smp_processor_id();
505 header
->ph_sec
= (__u32
)tv
.tv_sec
;
506 header
->ph_usec
= tv
.tv_usec
;
507 header
->ph_stack
= stack
;
508 header
->ph_pid
= current
->pid
;
509 header
->ph_line_num
= line
;
515 trace_print_to_console(struct spl_debug_header
*hdr
, int mask
, const char *buf
,
516 int len
, const char *file
, const char *fn
)
518 char *prefix
= "SPL", *ptype
= NULL
;
520 if ((mask
& SD_EMERG
) != 0) {
523 } else if ((mask
& SD_ERROR
) != 0) {
526 } else if ((mask
& SD_WARNING
) != 0) {
528 ptype
= KERN_WARNING
;
529 } else if ((mask
& (SD_CONSOLE
| spl_debug_printk
)) != 0) {
534 if ((mask
& SD_CONSOLE
) != 0) {
535 printk("%s%s: %.*s", ptype
, prefix
, len
, buf
);
537 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype
, prefix
,
538 hdr
->ph_pid
, hdr
->ph_stack
, file
,
539 hdr
->ph_line_num
, fn
, len
, buf
);
546 trace_max_debug_mb(void)
548 return MAX(512, ((num_physpages
>> (20 - PAGE_SHIFT
)) * 80) / 100);
551 static struct trace_page
*
555 struct trace_page
*tage
;
557 page
= alloc_pages(gfp
| __GFP_NOWARN
, 0);
561 tage
= kmalloc(sizeof(*tage
), gfp
);
563 __free_pages(page
, 0);
568 atomic_inc(&trace_tage_allocated
);
574 tage_free(struct trace_page
*tage
)
576 __ASSERT(tage
!= NULL
);
577 __ASSERT(tage
->page
!= NULL
);
579 __free_pages(tage
->page
, 0);
581 atomic_dec(&trace_tage_allocated
);
584 static struct trace_page
*
585 tage_from_list(struct list_head
*list
)
587 return list_entry(list
, struct trace_page
, linkage
);
591 tage_to_tail(struct trace_page
*tage
, struct list_head
*queue
)
593 __ASSERT(tage
!= NULL
);
594 __ASSERT(queue
!= NULL
);
596 list_move_tail(&tage
->linkage
, queue
);
599 /* try to return a page that has 'len' bytes left at the end */
600 static struct trace_page
*
601 trace_get_tage_try(struct trace_cpu_data
*tcd
, unsigned long len
)
603 struct trace_page
*tage
;
605 if (tcd
->tcd_cur_pages
> 0) {
606 __ASSERT(!list_empty(&tcd
->tcd_pages
));
607 tage
= tage_from_list(tcd
->tcd_pages
.prev
);
608 if (tage
->used
+ len
<= PAGE_SIZE
)
612 if (tcd
->tcd_cur_pages
< tcd
->tcd_max_pages
) {
613 if (tcd
->tcd_cur_stock_pages
> 0) {
614 tage
= tage_from_list(tcd
->tcd_stock_pages
.prev
);
615 tcd
->tcd_cur_stock_pages
--;
616 list_del_init(&tage
->linkage
);
618 tage
= tage_alloc(GFP_ATOMIC
);
621 "failure to allocate a tage (%ld)\n",
628 tage
->cpu
= smp_processor_id();
629 tage
->type
= tcd
->tcd_type
;
630 list_add_tail(&tage
->linkage
, &tcd
->tcd_pages
);
631 tcd
->tcd_cur_pages
++;
639 /* return a page that has 'len' bytes left at the end */
640 static struct trace_page
*
641 trace_get_tage(struct trace_cpu_data
*tcd
, unsigned long len
)
643 struct trace_page
*tage
;
645 __ASSERT(len
<= PAGE_SIZE
);
647 tage
= trace_get_tage_try(tcd
, len
);
651 if (tcd
->tcd_cur_pages
> 0) {
652 tage
= tage_from_list(tcd
->tcd_pages
.next
);
654 tage_to_tail(tage
, &tcd
->tcd_pages
);
661 spl_debug_msg(void *arg
, int subsys
, int mask
, const char *file
,
662 const char *fn
, const int line
, const char *format
, ...)
664 spl_debug_limit_state_t
*cdls
= arg
;
665 struct trace_cpu_data
*tcd
= NULL
;
666 struct spl_debug_header header
= { 0, };
667 struct trace_page
*tage
;
668 /* string_buf is used only if tcd != NULL, and is always set then */
669 char *string_buf
= NULL
;
672 int needed
= 85; /* average message length */
678 subsys
= SS_DEBUG_SUBSYS
;
683 if (strchr(file
, '/'))
684 file
= strrchr(file
, '/') + 1;
686 trace_set_debug_header(&header
, subsys
, mask
, line
, 0);
688 tcd
= trace_get_tcd();
692 if (tcd
->tcd_shutting_down
) {
698 known_size
= strlen(file
) + 1;
700 known_size
+= strlen(fn
) + 1;
702 if (spl_debug_binary
)
703 known_size
+= sizeof(header
);
705 /* '2' used because vsnprintf returns real size required for output
706 * _without_ terminating NULL. */
707 for (i
= 0; i
< 2; i
++) {
708 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
710 if (needed
+ known_size
> PAGE_SIZE
)
718 string_buf
= (char *)page_address(tage
->page
) +
719 tage
->used
+ known_size
;
721 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
723 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
732 va_start(ap
, format
);
733 needed
+= vsnprintf(string_buf
, max_nob
, format
, ap
);
737 if (needed
< max_nob
)
741 header
.ph_len
= known_size
+ needed
;
742 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
744 if (spl_debug_binary
) {
745 memcpy(debug_buf
, &header
, sizeof(header
));
746 tage
->used
+= sizeof(header
);
747 debug_buf
+= sizeof(header
);
750 strcpy(debug_buf
, file
);
751 tage
->used
+= strlen(file
) + 1;
752 debug_buf
+= strlen(file
) + 1;
755 strcpy(debug_buf
, fn
);
756 tage
->used
+= strlen(fn
) + 1;
757 debug_buf
+= strlen(fn
) + 1;
760 __ASSERT(debug_buf
== string_buf
);
762 tage
->used
+= needed
;
763 __ASSERT (tage
->used
<= PAGE_SIZE
);
766 if ((mask
& spl_debug_printk
) == 0) {
767 /* no console output requested */
774 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
775 !time_before(cdls
->cdls_next
, jiffies
)) {
776 /* skipping a console message */
783 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
784 (10 * HZ
), jiffies
)) {
785 /* last timeout was a long time ago */
786 cdls
->cdls_delay
/= spl_console_backoff
* 4;
788 cdls
->cdls_delay
*= spl_console_backoff
;
790 if (cdls
->cdls_delay
< spl_console_min_delay
)
791 cdls
->cdls_delay
= spl_console_min_delay
;
792 else if (cdls
->cdls_delay
> spl_console_max_delay
)
793 cdls
->cdls_delay
= spl_console_max_delay
;
796 /* ensure cdls_next is never zero after it's been seen */
797 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
801 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
804 string_buf
= trace_get_console_buffer();
807 if (format
!= NULL
) {
808 va_start(ap
, format
);
809 needed
+= vsnprintf(string_buf
,
810 TRACE_CONSOLE_BUFFER_SIZE
, format
, ap
);
813 trace_print_to_console(&header
, mask
,
814 string_buf
, needed
, file
, fn
);
816 trace_put_console_buffer(string_buf
);
819 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
820 string_buf
= trace_get_console_buffer();
822 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
823 "Skipped %d previous similar message%s\n",
824 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
826 trace_print_to_console(&header
, mask
,
827 string_buf
, needed
, file
, fn
);
829 trace_put_console_buffer(string_buf
);
830 cdls
->cdls_count
= 0;
835 EXPORT_SYMBOL(spl_debug_msg
);
837 /* Do the collect_pages job on a single CPU: assumes that all other
838 * CPUs have been stopped during a panic. If this isn't true for
839 * some arch, this will have to be implemented separately in each arch.
842 collect_pages_from_single_cpu(struct page_collection
*pc
)
844 struct trace_cpu_data
*tcd
;
847 tcd_for_each(tcd
, i
, j
) {
848 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
849 tcd
->tcd_cur_pages
= 0;
854 collect_pages_on_all_cpus(struct page_collection
*pc
)
856 struct trace_cpu_data
*tcd
;
859 spin_lock(&pc
->pc_lock
);
860 for_each_possible_cpu(cpu
) {
861 tcd_for_each_type_lock(tcd
, i
, cpu
) {
862 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
863 tcd
->tcd_cur_pages
= 0;
866 spin_unlock(&pc
->pc_lock
);
870 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
872 INIT_LIST_HEAD(&pc
->pc_pages
);
874 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
875 collect_pages_from_single_cpu(pc
);
877 collect_pages_on_all_cpus(pc
);
881 put_pages_back_on_all_cpus(struct page_collection
*pc
)
883 struct trace_cpu_data
*tcd
;
884 struct list_head
*cur_head
;
885 struct trace_page
*tage
;
886 struct trace_page
*tmp
;
889 spin_lock(&pc
->pc_lock
);
891 for_each_possible_cpu(cpu
) {
892 tcd_for_each_type_lock(tcd
, i
, cpu
) {
893 cur_head
= tcd
->tcd_pages
.next
;
895 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
897 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
900 tage_to_tail(tage
, cur_head
);
901 tcd
->tcd_cur_pages
++;
906 spin_unlock(&pc
->pc_lock
);
910 put_pages_back(struct page_collection
*pc
)
912 if (!spl_panic_in_progress
)
913 put_pages_back_on_all_cpus(pc
);
917 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
919 struct page_collection pc
;
921 struct trace_page
*tage
;
922 struct trace_page
*tmp
;
926 down_write(&trace_sem
);
928 filp
= spl_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
932 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
937 spin_lock_init(&pc
.pc_lock
);
938 collect_pages(dp
, &pc
);
939 if (list_empty(&pc
.pc_pages
)) {
947 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
948 rc
= spl_filp_write(filp
, page_address(tage
->page
),
949 tage
->used
, spl_filp_poff(filp
));
950 if (rc
!= (int)tage
->used
) {
951 printk(KERN_WARNING
"SPL: Wanted to write %u "
952 "but wrote %d\n", tage
->used
, rc
);
954 __ASSERT(list_empty(&pc
.pc_pages
));
957 list_del(&tage
->linkage
);
963 rc
= spl_filp_fsync(filp
, 1);
965 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
967 spl_filp_close(filp
);
969 up_write(&trace_sem
);
975 spl_debug_flush_pages(void)
978 struct page_collection pc
;
979 struct trace_page
*tage
;
980 struct trace_page
*tmp
;
982 spin_lock_init(&pc
.pc_lock
);
983 init_waitqueue_head(&dp
.dp_waitq
);
984 dp
.dp_pid
= current
->pid
;
986 atomic_set(&dp
.dp_done
, 0);
988 collect_pages(&dp
, &pc
);
989 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
990 list_del(&tage
->linkage
);
996 spl_debug_set_mask(unsigned long mask
) {
997 spl_debug_mask
= mask
;
1000 EXPORT_SYMBOL(spl_debug_set_mask
);
1003 spl_debug_get_mask(void) {
1004 return spl_debug_mask
;
1006 EXPORT_SYMBOL(spl_debug_get_mask
);
1009 spl_debug_set_subsys(unsigned long subsys
) {
1010 spl_debug_subsys
= subsys
;
1013 EXPORT_SYMBOL(spl_debug_set_subsys
);
1016 spl_debug_get_subsys(void) {
1017 return spl_debug_subsys
;
1019 EXPORT_SYMBOL(spl_debug_get_subsys
);
1022 spl_debug_set_mb(int mb
)
1025 int limit
= trace_max_debug_mb();
1026 struct trace_cpu_data
*tcd
;
1028 if (mb
< num_possible_cpus()) {
1029 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1030 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1035 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1036 "%dMB - upper limit is %d\n", mb
, limit
);
1040 mb
/= num_possible_cpus();
1041 pages
= mb
<< (20 - PAGE_SHIFT
);
1043 down_write(&trace_sem
);
1045 tcd_for_each(tcd
, i
, j
)
1046 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1048 up_write(&trace_sem
);
1052 EXPORT_SYMBOL(spl_debug_set_mb
);
1055 spl_debug_get_mb(void)
1058 struct trace_cpu_data
*tcd
;
1059 int total_pages
= 0;
1061 down_read(&trace_sem
);
1063 tcd_for_each(tcd
, i
, j
)
1064 total_pages
+= tcd
->tcd_max_pages
;
1066 up_read(&trace_sem
);
1068 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1070 EXPORT_SYMBOL(spl_debug_get_mb
);
1072 void spl_debug_dumpstack(struct task_struct
*tsk
)
1074 extern void show_task(struct task_struct
*);
1079 printk("SPL: Showing stack for process %d\n", tsk
->pid
);
1082 EXPORT_SYMBOL(spl_debug_dumpstack
);
1084 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1086 spl_debug_catastrophe
= 1;
1087 spl_debug_msg(NULL
, 0, SD_EMERG
, file
, func
, line
, "SPL PANIC\n");
1090 panic("SPL PANIC in interrupt.\n");
1092 if (in_atomic() || irqs_disabled())
1093 flags
|= DL_NOTHREAD
;
1095 /* Ensure all debug pages and dumped by current cpu */
1096 if (spl_debug_panic_on_bug
)
1097 spl_panic_in_progress
= 1;
1099 spl_debug_dumpstack(NULL
);
1100 spl_debug_dumplog(flags
);
1102 if (spl_debug_panic_on_bug
)
1105 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1109 EXPORT_SYMBOL(spl_debug_bug
);
1112 spl_debug_clear_buffer(void)
1114 spl_debug_flush_pages();
1117 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1120 spl_debug_mark_buffer(char *text
)
1122 SDEBUG(SD_WARNING
, "*************************************\n");
1123 SDEBUG(SD_WARNING
, "DEBUG MARKER: %s\n", text
);
1124 SDEBUG(SD_WARNING
, "*************************************\n");
1128 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1131 trace_init(int max_pages
)
1133 struct trace_cpu_data
*tcd
;
1136 init_rwsem(&trace_sem
);
1138 /* initialize trace_data */
1139 memset(trace_data
, 0, sizeof(trace_data
));
1140 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1141 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1142 NR_CPUS
, GFP_KERNEL
);
1143 if (trace_data
[i
] == NULL
)
1147 tcd_for_each(tcd
, i
, j
) {
1148 spin_lock_init(&tcd
->tcd_lock
);
1149 tcd
->tcd_pages_factor
= pages_factor
[i
];
1152 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1153 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1154 tcd
->tcd_cur_pages
= 0;
1155 tcd
->tcd_cur_stock_pages
= 0;
1156 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1157 tcd
->tcd_shutting_down
= 0;
1160 for (i
= 0; i
< num_possible_cpus(); i
++) {
1161 for (j
= 0; j
< 3; j
++) {
1162 trace_console_buffers
[i
][j
] =
1163 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1166 if (trace_console_buffers
[i
][j
] == NULL
)
1174 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1179 spl_debug_init(void)
1181 int rc
, max
= spl_debug_mb
;
1183 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1184 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1186 /* If spl_debug_mb is set to an invalid value or uninitialized
1187 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1188 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1189 max
>= 512 || max
< 0) {
1190 max
= TCD_MAX_PAGES
;
1192 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1195 rc
= trace_init(max
);
1203 trace_cleanup_on_all_cpus(void)
1205 struct trace_cpu_data
*tcd
;
1206 struct trace_page
*tage
;
1207 struct trace_page
*tmp
;
1210 for_each_possible_cpu(cpu
) {
1211 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1212 tcd
->tcd_shutting_down
= 1;
1214 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1216 list_del(&tage
->linkage
);
1219 tcd
->tcd_cur_pages
= 0;
1229 trace_cleanup_on_all_cpus();
1231 for (i
= 0; i
< num_possible_cpus(); i
++) {
1232 for (j
= 0; j
< 3; j
++) {
1233 if (trace_console_buffers
[i
][j
] != NULL
) {
1234 kfree(trace_console_buffers
[i
][j
]);
1235 trace_console_buffers
[i
][j
] = NULL
;
1240 for (i
= 0; i
< TCD_TYPE_MAX
&& trace_data
[i
] != NULL
; i
++) {
1241 kfree(trace_data
[i
]);
1242 trace_data
[i
] = NULL
;
1247 spl_debug_fini(void)